package main
type Key struct {
stuff1 string
stuff2 []string
}
type Val struct {
}
type MyMap struct {
map1 map[Key]*Val // compiles fine!
}
func main() {
var map2 map[Key]*Val // "invalid map key type Key"
}
这是正确的行为,还是go编译器中的错误?
我在Linux x64上使用go-1.1。
答案 0 :(得分:4)
编译器是对的。来自规格:Map Types:
必须为键类型的操作数完全定义比较运算符==和!=;因此,键类型不能是函数,映射或切片。
如果密钥类型是所有结构字段的结构,则此限制适用于传递,它们也必须遵守上述引用的规则,
stuff2 []string
没有。
编辑:
关于map1
没有被标记的问题,可能 是一个错误,可能是由于MyMap从未被引用而引起的,因此可能会跳过它的类型检查。