create a map with a key of type array in go

To create a map in Go with a key of type array, we need to use an array as the key when defining our map. Here is an example:

main.go
package main

import "fmt"

func main() {
    m := make(map[[2]int]string)

    m[[2]int{0, 0}] = "hello"
    m[[2]int{1, 0}] = "world"

    fmt.Println(m[[2]int{0, 0}]) // Output: hello
    fmt.Println(m[[2]int{1, 0}]) // Output: world
}
239 chars
14 lines

In this code, we create a map with a key of type [2]int (an array with two integers). We can then access and update the values in the map using arrays with the same type.

This technique can be used with arrays of any length to create a map with an array key.

related categories

gistlibby LogSnag