比如存在下面两个类型 A
和 B
:
type A struct {
x, y int
}
type B struct {
x, y int
}
此时,类型 A
和类型 B
对应的结构体成员是一样的,那么,A 和 B 拥有的是不是 同一个 底层类型呢?
就像这样:
A B
\ /
\ /
\/
struct {
x, y int
}
比如存在下面两个类型 A
和 B
:
type A struct {
x, y int
}
type B struct {
x, y int
}
此时,类型 A
和类型 B
对应的结构体成员是一样的,那么,A 和 B 拥有的是不是 同一个 底层类型呢?
就像这样:
A B
\ /
\ /
\/
struct {
x, y int
}
Two struct types are identical if they have the same sequence of fields, and if corresponding fields have the same names, and identical types, and identical tags.Non-exported field names from different packages are always different.
7 回答5.3k 阅读
6 回答6.8k 阅读✓ 已解决
4 回答2.3k 阅读
1 回答3.3k 阅读
2 回答922 阅读✓ 已解决
2 回答2.2k 阅读
1 回答2.2k 阅读
如果 A B 在同一个包里,是的。
如果他们不在同一个包里,那么由于 struct 里有没有被导出的 field ,他们的 underlying type 是不同的。
Type Identity