Going to correct compiler behavior or error?

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 behavior or error in the go compiler?

I am using go-1.1 on Linux x64.

+4
source share
1 answer

The compiler is right. From the specifications: Card types :

Comparison operators == and! = Must be fully defined for key type operands; therefore, the key type should not be a function, map, or slice.

This restriction is applied transitively, if the key type is a structure to all fields of the structure, they must also observe the above rule, which

 stuff2 []string 

no.

EDIT:

As for map1 not tagged, perhaps this is an error, possibly due to the fact that MyMap was never referenced and, therefore, type checking was probably skipped.

+4
source

Source: https://habr.com/ru/post/1485985/


All Articles