Which types are comparable in Go and can be used as map keys or with the == operator?
Last updated
Comparable (usable with
==and as map keys): booleans, numerics, strings, pointers, channels, interfaces, and structs/arrays whose fields/elements are all comparable.Slices, maps, and functions are not comparable: they can only be compared to
nil, somap[[]byte]intwon't compile.Interface comparison panics at runtime if the dynamic type isn't comparable. In generics, the
comparableconstraint restricts type parameters to==-able types.