dictionary - 如何在Golang中使用与 map 中的键相同的数据来区分两种自定义结构类型?

标签 dictionary go struct

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==ttrue。结构值本身就是映射键。因此,在您的示例中, map 的末尾只有一个元素。

有关可比性的更多信息:

https://golang.org/ref/spec#Comparison_operators

Go中没有等效的hashCode

关于dictionary - 如何在Golang中使用与 map 中的键相同的数据来区分两种自定义结构类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59851093/

相关文章:

asp.net-mvc - MVC 5 : dictionary in model bound to a series of checkboxes in view?

python - 如何对不同大小的不同文件进行匹配

go - 从Wasm读取cookie

string - Slice 不会在长度上出现 panic ,但在获取索引等于长度的值时会出现 panic

c++ - 为什么 const 结构数组在按名称引用 const 结构时不放在 .rodata 中?

ios - 如何使用 Swift 找到距我所在位置的距离

python - `**` 在表达式 `dict(d1, **d2)` 中是什么意思?

performance - 知道为什么 Go 在递归斐波那契上似乎相对较慢吗?

C++ 访问 union 结构中的变量

c++ - 如何释放 C++ 中的结构和对象数组?