Go Maps Tutorial

In this Go tutorial we learn about the Map collection type that can store multiple values as key/value pairs in a single container.

We learn how to define an empty Map or initialize a Map with key/value pairs. We also learn how to add, access and delete individual key/value pairs.

What is a Map

A Map is a collection of key/value pairs, similar to an array.

A Map doesn’t have a numerical index, instead we define custom keys (like words) that correspond to values.

How to define a Map

We define a map with the make() function. First, we create a variable and assign the result of the make function to it.

As argument we specify the map keyword, followed by what type the keys will be between square brackets, and lastly, what type the values will be.

Syntax:
 var map_name = make( map [keys_type] values_type)
Example:
package main

func main() {

    var m1 = make(map [string] int)
}

In the example above, we define a map called m1 with strings as keys and ints as values.

How to add key/value pairs to a Map

To add a key/value pair to a Map we use the map name, followed by a key in square brackets and assign a value to it with the assignment operator.

Syntax:
 map_name[key] = value
Example:
package main

import "fmt"

func main() {

    var m1 = make(map [string] int)

    m1["John"] = 1
    m1["Jane"] = 2
}

The keys and values must match the types specified when we defined the Map.

How to access key/value pairs in a Map

To access a value in a map, we refer to the map name and the key of the value we want to access between square brackets.

Syntax:
 map_name[key]
Example:
package main

import "fmt"

func main() {

    var m1 = make(map [string] int)

    m1["John"] = 1
    m1["Jane"] = 2

    fmt.Println(m1["John"])
    fmt.Println(m1["Jane"])
}

How to initialize a Map with values

If we know the key/value pairs we want to add beforehand, we can initialize the Map with values when we declare it.

First, we assign the map to a variable, then we use the keyword map followed by the key type in square brackets and the value type. Then, we create a code block for it with open and close curly braces.

Inside the code block we specify our key, followed by a colon and the value that corresponds to it. Multiple key/value pairs are separated with commas.

Syntax:
map_name := map [key_type] value_type {

    key : value,
    key : value,
}
Example:
package main

import "fmt"

func main() {

    m1 := map [string] int {
        "John" : 1,
        "Jane" : 2,
    }

    fmt.Println(m1["John"])
    fmt.Println(m1["Jane"])
}

In the example above, we initialize the map with the same values as before, but this time we use initialization syntax.

How to delete a key/value pair in a Map

To delete an entry from a map, we have to use the delete() function.

As arguments, the function takes the map name and the key of the value we want to delete.

Example:
package main

import "fmt"

func main() {

    m1 := map [string] int {
        "John" : 1,
        "Jane" : 2,
        "Jack" : 3,
        "Jill" : 4,
    }

    fmt.Printf("Before deleted entry:\n")
    for x := range m1 {
        fmt.Println(m1[x])
    }

    // delete value where
    // 'Jack' is the key
    delete(m1, "Jack")

    fmt.Printf("\nAfter deleted entry:\n")
    for x := range m1 {
        fmt.Println(m1[x])
    }
}

Summary: Points to remember

  • A Map is a collection of key/value pairs similar to an array.
  • A Map is defined with the make() function.
  • When making a Map, we have to specify both the types the keys will be, and the types the values will be.
  • We can add key/value pairs by simply specifying a key we want and then assign a value to it.
    • The keys and values must match those specified when the Map was defined.
  • We can initialize a Map with values by adding them in a code block.
  • To delete a key/value pair we use the delete() function with the map name and key name as arguments.