Why doesn't this work?
package main
type Word uint8
type Memory []Word
func main() {
bytes := []uint8{}
memory := Memory{}
bytes = memory
}
The compiler generates this error:
9:9: cannot use memory (type Memory) as type []byte in assignment
As I understand it, []uint8
and Memory
should be mutually assignable.
Here are the assignability rules
In this particular case none of those is held, so the types are not assignable.
Given you mentioned this answer is not detailed enough - let's run through every assignability rule: (for ease let's use A
as a substitute for []uint8
and B
as a substitute for []Word
)
A
is not identical to B
nil
valueSo, as you can see - there is no way A
is assignable to B
.
If you declare Memory as type Memory []uint8
it would work (as @RayfenWindspear mentioned in the comments), I'm sure though the question roots is more "why" not "how to fix".