如何防止将类型用作映射键?
How can I prevent a type being used as a map key?
我有一个可以用作映射键的类型,但我想防止这种情况发生。我假设如果该类型包含一个私有成员,那么其他包就不可能,但这似乎仍然有效。使类型无法用作映射键的最佳方法是什么?
type MyType struct {
A *A
b b
preventUseAsKey ?
}
您的类型应该 not be comparable 才能不适合作为地图键。
Slice, map, and function values are not comparable
参见Key Type:
Notably absent from the list are slices, maps, and functions; these types cannot be compared using ==
, and may not be used as map keys.
因此,如果您的类型是切片、映射或函数,您应该得到您需要的。
它可以是一个 "alias"(定义一个新的 named type):
type StringSliceWrap []string
type MyFunc func(i int)
该别名不会用作映射键。
2017 年更新:Brad Fitzpatrick give this tip (adding a slice in your struct
) to make sure your type struct
is not comparable: See play.golang.org:
package main
// disallowEqual is an uncomparable type.
// If you place it first in your struct, you prevent == from
// working on your struct without growing its size. (Don't put it
// at the end; that grows the size of the struct)
type disallowEqual [0]func()
type T struct {
_ disallowEqual
Foo string
Bar int
}
func main() {
var t1 T
var t2 T
println(t1 == t2)
}
T
现在不能用作放大器键!
我没有看到禁止将类型用作键的任何好处。这只是一个选项,可以使用也可以不使用,类型不会因为您禁止将其用作地图键而变得更好或更小或更快。
但是如果你想这样做: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.
因此,例如添加一个类型为切片的字段,就完成了:
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
类型用作键类型。
我有一个可以用作映射键的类型,但我想防止这种情况发生。我假设如果该类型包含一个私有成员,那么其他包就不可能,但这似乎仍然有效。使类型无法用作映射键的最佳方法是什么?
type MyType struct {
A *A
b b
preventUseAsKey ?
}
您的类型应该 not be comparable 才能不适合作为地图键。
Slice, map, and function values are not comparable
参见Key Type:
Notably absent from the list are slices, maps, and functions; these types cannot be compared using
==
, and may not be used as map keys.
因此,如果您的类型是切片、映射或函数,您应该得到您需要的。
它可以是一个 "alias"(定义一个新的 named type):
type StringSliceWrap []string
type MyFunc func(i int)
该别名不会用作映射键。
2017 年更新:Brad Fitzpatrick give this tip (adding a slice in your struct
) to make sure your type struct
is not comparable: See play.golang.org:
package main
// disallowEqual is an uncomparable type.
// If you place it first in your struct, you prevent == from
// working on your struct without growing its size. (Don't put it
// at the end; that grows the size of the struct)
type disallowEqual [0]func()
type T struct {
_ disallowEqual
Foo string
Bar int
}
func main() {
var t1 T
var t2 T
println(t1 == t2)
}
T
现在不能用作放大器键!
我没有看到禁止将类型用作键的任何好处。这只是一个选项,可以使用也可以不使用,类型不会因为您禁止将其用作地图键而变得更好或更小或更快。
但是如果你想这样做: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.
因此,例如添加一个类型为切片的字段,就完成了:
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
类型用作键类型。