Array types (unlike slices) in Go are comparable, so there is nothing magical in it: you can just define it like any other maps: map[KeyType]ValueType
where KeyType
will be [3]int
and ValueType
will be string
.
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.
m := map[[3]int]string{}
m[[3]int{1, 2, 3}] = "First quarter"
m[[3]int{4, 5, 6}] = "Second quarter"
m[[3]int{7, 8, 9}] = "Third quarter"
m[[3]int{10, 11, 12}] = "Fourth quarter"
fmt.Println(m)
Output:
map[[1 2 3]:First quarter [4 5 6]:Second quarter
[7 8 9]:Third quarter [10 11 12]:Fourth quarter]
Try it on the Go Playground.
To query an element:
fmt.Println(m[[3]int{1, 2, 3}]) // Prints "First quarter"
You can also create the map in one step:
m := map[[3]int]string{
[3]int{1, 2, 3}: "First quarter",
[3]int{4, 5, 6}: "Second quarter",
[3]int{7, 8, 9}: "Third quarter",
[3]int{10, 11, 12}: "Fourth quarter",
}