Maps in Go

Alissa LozhkinAlissa Lozhkin
3 min read

Maps in Go are the exact same as dictionaries in Python in that they store several key-value pairs. In this post, I will go over how to initialize and use maps in Go.

The basic syntax for creating a map is as follows:

map1 := map[key_datatype]value_datatype{key-value pairs}

The code block below demonstrates how one can create a map with strings mapping to integers.

import "fmt"

map2 := map[string]int{}  // an empty map
map3 := map[string]int{"a": 1, "b": 2}  // a non-empty map

fmt.Println(map2)
fmt.Println(map3)
// output: 
//    map[]
//    map[a:1 b:2]

Once a map is initialized, there are many things that we can do with it. For instance, we can access the value of a certain key in the map by doing the following:

import "fmt"

map4 := map[string]int{"x": 1, "y": 2}

fmt.Println(map4["x"])
// output = 1

You may think that trying to access the value of a key that does not exist in the map will give you an error (like in Python). However, in Go, the value returned will be 0 (if the value datatype is an integer), 0.0 (if the value datatype is a float), or "" (if the value datatype is a string). This may be confusing because an actually existing key in the map may have the value 0. Therefore, to determine whether a key is in a map, we can do the following:

import "fmt"

map5 := map[string]int{"x": 1, "y": 2}
_, exists_x := map5["x"]
_, exists_z := map5["z"]

fmt.Println(exists_x)
fmt.Println(exists_z)
// output =
//    true
//    false

The '_' seen in the examples above is known as the blank identifier . Both examples have two parameters returned, the value of the key and whether the key is in the map. Since only the latter is used in the code, we can disregard the former by using '_'.

We can also assign a new value to a key in the map. The code block below demonstrates how we can do this:

import "fmt"

map5 := map[string]int{"animals": 4, "plants": 3}
map5["animals"] = 10

fmt.Println(map5["animals"])
// output = 10

We can also add/delete a key from the map:

import "fmt"

// adding a key
map6 := map[string]int{"lions": 1, "buffalo": 2}
map6["giraffes"] = 3
fmt.Println(map6)
// output: map[buffalo:2 giraffes:3 lions:1]

// deleting a key
delete(map6, "lions")  // "delete" function takes in a map and a key
fmt.Println(map6)
// output: map[buffalo:2 giraffes:3]

Note that the map is outputted with the keys in alphabetical order.

We can also iterate through a map using the for-each range loop (see this post for more details).

map7 := map[string]int{"x": 5, "y": 6}
for k, v := range map7 {
    fmt.Println(k, v)  // print both the key and value side-by-side
}
// output: 
//    x 5
//    y 6
0
Subscribe to my newsletter

Read articles from Alissa Lozhkin directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Alissa Lozhkin
Alissa Lozhkin

My name is Alissa and I am a fourth-year computer science and math student at the University of Toronto! I am very interested in computer science and software development, and I love learning about new technologies! Get in touch with me on LinkedIn: https://www.linkedin.com/in/alissa-lozhkin