如何理解两个命名类型在golang中是相同的

lia*_*ggo 4 types go

类型标识规则指出:

Two named types are identical if their type names originate in the same TypeSpec

我不太明白两个类型名称是如何来自同一个TypeSpec的.你能解释一下或给我看一个例子吗?

Kei*_*all 7

只有一个类型名称可以源自TypeSpec.这就是重点.所以

type Foo int64
var x Foo
var y Foo
Run Code Online (Sandbox Code Playgroud)

然后两个Foos都来自相同的TypeSpec,所以它们是相同Foo的.

但是,如果您有两个不同的文件(在不同的包中):

前:

type Foo int64
var x Foo
Run Code Online (Sandbox Code Playgroud)

b.go:

type Foo int64
var y Foo
Run Code Online (Sandbox Code Playgroud)

那么这两种Foo情况就不一样了.即使它们是相同的类型名称,它们也来自不同的TypeSpecs.其结果是类型xy不相同(因此x = y不允许使用演员表).