将int8放入字节数组

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?

Try this:

buf := make([]byte, 1)
var value int8
value = 45
buf[0] = byte(value)

UPDATE: Took out the code converting negative numbers to positive ones. It appears that byte(...) already does this conversion in current versions of Go.

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