ZetCode

Go map

last modified April 11, 2024

In this article we show how to work with maps in Golang.

A map is an unordered collection of key/value pairs, where each key is unique. Maps are also called dictionaries, associative arrays, or hash tables. The size of the map (the number of key-value pairs) is determined with the len function.

Maps are created with make function or with map literals.

To remove a pair, we can use the built-in delete function. The default zero value of a map is nil.

$ go version
go version go1.22.2 linux/amd64

We use Go version 1.22.2.

Go map init

The make function creates an empty map.

init.go
package main

import "fmt"

func main() {

    var benelux map[string]string

    benelux = make(map[string]string)

    benelux["be"] = "Belgium"
    benelux["nl"] = "Netherlands"
    benelux["lu"] = "Luxembourgh"

    fmt.Println(benelux)
    fmt.Printf("%q\n", benelux)
}

We have a map of Benelux states.

var benelux map[string]string

A map of string keys and string values is declared.

benelux = make(map[string]string)

With the make function, we create an empty map.

benelux["be"] = "Belgium"
benelux["nl"] = "Netherlands"
benelux["lu"] = "Luxembourgh"

We add three pairs to the map.

$ go run init.go
map[be:Belgium lu:Luxembourgh nl:Netherlands]
map["be":"Belgium" "lu":"Luxembourgh" "nl":"Netherlands"]
init2.go
package main

import "fmt"

func main() {

    benelux := make(map[string]string)

    benelux["be"] = "Belgium"
    benelux["nl"] = "Netherlands"
    benelux["lu"] = "Luxembourgh"

    fmt.Println(benelux)
    fmt.Printf("%q\n", benelux)
}

We can shorten the syntax by using the := operator.

Go map literal

To create and initialze a map with literal notation, we specify the key/value pairs inside the curly {} brackets. Keys and values are separated with a colon character. Each pair is separated with a comma.

literal.go
package main

import "fmt"

func main() {

    m := map[string]float64{
        "pi":  3.14159,
        "e":   2.71828,
        "ln2": 0.69314,
    }

    fmt.Println(m)
}

In the code example, we create a map of constants using map literal syntax.

Go map size

The size of the map is determined with the len function. It returns the number of pairs in the map.

length.go
package main

import "fmt"

func main() {

    countries := map[string]string{
        "sk": "Slovakia",
        "ru": "Russia",
        "de": "Germany",
        "no": "Norway",
    }

    fmt.Printf("There are %d pairs in the map\n", len(countries))
}

In the code example, we create a map of countries, where the country codes are keys and the country names are values. We print the number of countries in the map.

$ go run length.go
There are 4 pairs in the map

There are four key/value pairs in the map.

Go map loop

With for and range keywords, we can loop over map elements.

loop.go
package main

import "fmt"

func main() {

    countries := map[string]string{
        "sk": "Slovakia",
        "ru": "Russia",
        "de": "Germany",
        "no": "Norway",
    }

    for country := range countries {
        fmt.Println(country, "=>", countries[country])
    }

    for key, value := range countries {
        fmt.Printf("countries[%s] = %s\n", key, value)
    }
}

In the code example, we loop over countries map in two ways.

for country := range countries {
    fmt.Println(country, "=>", countries[country])
}

In the first case, we loop by pair objects.

for key, value := range countries {
    fmt.Printf("countries[%s] = %s\n", key, value)
}

In the second case, we loop by keys and values.

$ go run loop.go
de => Germany
no => Norway
sk => Slovakia
ru => Russia
countries[ru] = Russia
countries[de] = Germany
countries[no] = Norway
countries[sk] = Slovakia

Go map check element

We check if a value exists by referring to its key. If we have one variable on the left side, we either get an existing value or a zero value. If there are two variables on the left side, we get the value and a boolean value indicating the existence of the key in the map.

checking.go
package main

import "fmt"

func main() {

    grades := map[string]int{
        "Lucia": 2,
        "Paul":  1,
        "Merry": 3,
        "Jane":  1,
    }

    g := grades["Lucia"]
    fmt.Println(g)

    g = grades["Peter"]
    fmt.Println(g)

    g, found := grades["Lucia"]
    fmt.Println(g, found)

    g, found = grades["Peter"]
    fmt.Println(g, found)

    if g, found := grades["Jane"]; found {
        fmt.Println(g)
    }
}

In the code example, we check the grades of students.

g := grades["Lucia"]
fmt.Println(g)

Here we print the grade of Lucia.

g = grades["Peter"]
fmt.Println(g)

Since Peter is not in the map, we get a zero value (0 for integer).

g, found := grades["Lucia"]
fmt.Println(g, found)

g, found = grades["Peter"]
fmt.Println(g, found)

Here we get the grades and the boolean values as well.

if g, found := grades["Jane"]; found {
    fmt.Println(g)
}

This is a concise syntax for getting the boolean value and checking it in the if statement.

$ go run checking.go
2
0
2 true
0 false
1

Go map remove element

An element is removed from a map with the delete function.

removing.go
package main

import "fmt"

func main() {

    countries := map[string]string{
        "sk": "Slovakia",
        "ru": "Russia",
        "de": "Germany",
        "no": "Norway",
    }

    fmt.Println(countries)

    delete(countries, "ru")

    fmt.Println(countries)
}

In the code example, we remove one country from a map of countries.

$ go run removing.go
map[de:Germany no:Norway ru:Russia sk:Slovakia]
map[de:Germany no:Norway sk:Slovakia]

Russia was successfully removed.

Go map is a reference type

Go map is a reference type. It means that when we assign a reference to a new variable or pass a map to a function, the reference to the map is copied.

reference.go
package main

import "fmt"

func main() {

    countries := map[string]string{
        "sk": "Slovakia",
        "ru": "Russia",
        "de": "Germany",
        "no": "Norway",
    }

    countries2 := countries

    countries2["us"] = "USA"

    fmt.Println(countries)
}

Modifying countries2 map, which is a reference to the countries, the original map is modified as well.

Go map of structs

In the following example, we work with a map of structs. A struct is a user-defined type that contains a collection of fields. It is used to group related data to form a single unit.

users.go
package main

import "fmt"

type User struct {
    name       string
    occupation string
}

func main() {

    u1 := User{
        name:       "John Doe",
        occupation: "gardener",
    }

    u2 := User{
        name:       "Richard Roe",
        occupation: "driver",
    }

    u3 := User{
        name:       "Lucy Smith",
        occupation: "teacher",
    }

    users := map[int]User{
        1: u1,
        2: u2,
        3: u3,
    }

    fmt.Println(users)
}

In the code example, we have a map of users. Each user is represented by a Go struct.

$ go run users.go 
map[1:{John Doe gardener} 2:{Richard Roe driver} 3:{Lucy Smith teacher}]

Source

Go maps in action

In this article we have worked with map in Golang.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Go tutorials.