分配等效类型的切片不起作用

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)

  • x's type is identical to T. --- Nope, A is not identical to B
  • x's type V and T have identical underlying types and at least one of V or T is not a defined type. --- Nope, both are defined types
  • T is an interface type and x implements T. --- None of them is an interface
  • x is a bidirectional channel value, T is a channel type, x's type V and T have identical element types, and at least one of V or T is not a defined type. --- None of them is a channel
  • x is the predeclared identifier nil and T is a pointer, function, slice, map, channel, or interface type. --- None of them holds nil value
  • x is an untyped constant representable by a value of type T. --- Nope, no constants involved

So, 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".