A map is a collection of key value pairs, where you use the key to obtain the value that corresponds to it.

The preceding code declares a map where the keys are of type int, and the values are of type string.

var myMap map[int]string

Initialize a map using the make function:

myMap = make(map[int]string)

initialize the map with some values

myMap = map[int]string{1: “first”, 2: “Second”, 3: “third”}

To obtain a value from a map

//x will hold the value in “myMap” that corresponds to key 3
var x = myMap[3]

Check if a key exists in a map

//If the key 5 is not in “myMap”, then “ok” will be false
//Otherwise, “ok” will be true, and “x” will be the value
x,ok := myMap[5]