type Student struct {
id int,
name string,
}
//If I create two structs,
s := Student{1, "jack"}
t := Student{1, "jack"}
m := make(map[Student]bool)
m[s] = true
m[t] = true
1)可以吗?
2)在Golang中是否有类似于Java的hashCode?
最佳答案
如果两个结构值具有相同的成员值,并且所有这些成员都是可比较的,则这些结构相等。在您的示例中:s==t
是true
。结构值本身就是映射键。因此,在您的示例中, map 的末尾只有一个元素。
有关可比性的更多信息:
https://golang.org/ref/spec#Comparison_operators
Go中没有等效的hashCode
。
关于dictionary - 如何在Golang中使用与 map 中的键相同的数据来区分两种自定义结构类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59851093/