Why is `map[[]byte]something` not allowed?

I’ve just tried to declare map: map[[]byte]int and got compiler error:

./main.go:9:7: invalid map key type []byte

so my question is: why map key []byte isn’t allowed?

My guess is that because []byte variables are not comparable, and only comparable variables can be used as map keys. If you try to compare two []bytes, you will see that it’s an invalid operation (see a very simple example in playground).


From the language specification (emphasis mine):

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. If the key type is an interface type, these comparison operators must be defined for the dynamic key values; failure will cause a run-time panic.


okey, thank you

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.