Junior — Middle
Is it possible to use arrays as keys in a Map collection?
sobes.tech AI
Answer from AI
In Go, the keys in a map collection can only be types that support the comparison operation ==. Arrays in Go are comparable element-wise, so fixed-length arrays can be used as keys.
Example:
package main
import "fmt"
func main() {
m := make(map[[3]int]string)
key := [3]int{1, 2, 3}
m[key] = "example"
fmt.Println(m[key]) // Will output: example
}
However, slices ([]int) cannot be used as keys because they are not comparable. If you need a collection with keys based on slices, you should convert them to a string or another comparable type.