As said in The Go Programming Language on page 55: "an EXPLICIT conversion is required to convert a value from one type to another", even if both of them have same underlying type. For example:
type myByte byte
func main() {
var a byte
var b myByte
a = b // Compile error: cannot use b (type myByte) as type byte in assignment
a = byte(b) // OK
}
But for uint8
and byte
, I'm surprised that the conversion is implicit:
func main() {
var a byte
var b uint8
a = b // OK
b = a // OK
}
So why?
byte is an alias for uint8 and is equivalent to uint8 in all ways.
From GoDoc:
type Byte
byte is an alias for uint8 and is equivalent to uint8 in all ways. It is used, by convention, to distinguish byte values from 8-bit unsigned integer values.
type byte byte // Really: type byte = uint8 (see golang.org/issue/21601)
Earlier, on page 52 of The Go Programming Language, "the type byte is a synonym for uint8".
The Go Programming Language Specification
uint8 the set of all unsigned 8-bit integers (0 to 255) byte alias for uint8