Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting int8 into byte array

Tags:

go

I have the following byte array:

buf := make([]byte, 1)
var value int8
value = 45
buf[0] = value // cannot use type int8 as type []byte in assignment

And when I want to put a char value into the byte array I get the error that I cannot use type int8 as type []byte in assignment. What's wrong? How do I do this?

like image 336
user99999 Avatar asked Jun 24 '16 09:06

user99999


1 Answers

The issue you're having their is that although int8 and byte are roughly equivalent, they're not the same type. Go is a little stricter about this than, say, PHP (which isn't strict about much). You can get around this by explicitly casting the value to byte:

buf := make([]byte, 1)
var value int8
value = 45
buf[0] = byte(value) // cast int8 to byte
like image 132
Ross McFarlane Avatar answered Nov 15 '22 04:11

Ross McFarlane