我有一个可以用作映射键的类型,但我想防止这种情况发生。我假设如果该类型包含一个私有(private)成员,那么其他包就不可能,但这似乎仍然有效。使类型无法用作映射键的最佳方法是什么?
type MyType struct {
A *A
b b
preventUseAsKey ?
}
最佳答案
我没有看到禁止将类型用作键的任何好处。这只是一个选项,可以使用也可以不使用,类型不会因为您禁止将其用作映射键而更好或更小或更快。
但如果你想这样做:Spec: Map types:
The comparison operators == and != must be fully defined for operands of the key type; thus the key type must not be a function, map, or slice.
因此,如果您可以违反 comparison operators 的条款,你隐含地得到了你想要的。你有一个 struct
,struct
类型的术语:
Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.
因此 struct
值只有在所有字段都可比较时才可比较(因此只能用作映射中的键)。 只需添加一个类型不可比的字段。
Slice, map, and function values are not comparable.
因此,例如添加一个类型为 slice 的字段,就完成了:
type MyType struct {
S string
i int
notComparable []int
}
尝试使用上面的 MyType
作为键:
m := map[MyType]int{}
你得到一个编译时错误:
invalid map key type MyType
注意:
我写过禁止类型作为键没有任何好处。不仅如此:从现在开始,您将无法再对您的类型的值使用比较运算符(因为有额外的、不可比较的字段),例如你失去了比较这些值的选项:
p1, p2 := MyType{}, MyType{}
fmt.Println(p1 == p2)
编译时错误:
invalid operation: p1 == p2 (struct containing []int cannot be compared)
请注意,通过一些小技巧,您仍然可以保留类型的可比性,例如通过不导出您的类型而是嵌入原始类型的包装器类型;并将额外的、不可比较的类型添加到包装器类型,例如:
type myType struct {
S string
i int
}
type MyType struct {
myType
notComparable []int
}
func main() {
p1, p2 := MyType{}, MyType{}
fmt.Println(p1.myType == p2.myType)
}
这样,您的 myType
可以保持可比性,但仍然可以防止导出的包装器 MyType
类型用作键类型。
关于dictionary - 如何防止将类型用作映射键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37916647/