Senior
What data types can be used as keys for a map? Can an array be used as a key?
sobes.tech AI
Answer from AI
In Go, a key for a map can be any type that supports the comparison operations == and !=. This means that keys can be basic types (int, string, bool, etc.), pointers, structs if all their fields are comparable, and interfaces if the dynamic type is comparable.
Arrays in Go are comparable if their elements are comparable, so arrays can be used as keys in a map. For example:
m := make(map[[3]int]string)
m[[3]int{1, 2, 3}] = "value"
However, slices cannot be used as keys because they are not comparable (except when compared to nil).