Reputation:
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"
}
Is this the correct behaviour, or a bug in go compiler?
I am using go-1.1 on Linux x64.
Upvotes: 4
Views: 95
Reputation: 91193
The compiler is right. From the specs: 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.
This restriction applies transitively if the key type is a struct to all of the struct fields, they must obey the above quoted rule as well, which
stuff2 []string
does not.
EDIT:
What concerns map1
not being flagged, that probably is a bug, perhaps caused by MyMap never being referenced and thus it's type checking was probably skipped.
Upvotes: 4