Go Map

Summary: in this tutorial, you will learn about the Go Map type and how to use it to associate keys with values.

In Go, a map is a built-in type that associates keys with values. A map is similar to a hash table or dictionary in other programming languages.

Unlike arrays or slices, elements in a map do not have any particular order.

Declaring a map

To declare a map, you use a map keyword, followed by the type of keys, and the type of values:

var mapName map[keyType] valueTypeCode language: JavaScript (javascript)

For example:

var languages map[string] stringCode language: JavaScript (javascript)

In this example, we declare a map called languages with types of keys and values are string.

Alternatively, you can use the make() function to create a map:

var languages = make(map[string] string)Code language: JavaScript (javascript)

Initializing a map

To initialize a map, you can use a map literal as follows:

var languages = map[string]string {
    "go": "Go",
    "js": "javascript",
    "py": "python",
}Code language: JavaScript (javascript)

In this example, we specify a list of key/value pairs for the languages map, where keys are the file extensions and values are programming languages.

Note that the keys of a map must be unique. If you add a new element with an existing key, Go will overwrite the previous value.

Inside a function, you can declare and initialize a map using a short variable declaration as follows:

languages := map[string]string{
    "go": "Go",
    "js": "javascript",
    "py": "python",
}Code language: JavaScript (javascript)

Adding elements

To add an element to a map, you specify a key and a value:

mapName[key] = value

For example:

languages["php"] = "PHP"Code language: JavaScript (javascript)

If you use an existing key, Go will overwrite the previous value with the new value.

Retrieving elements

To retrieve a value by key, you specify the key inside the square brackets:

lang := languages["go"]Code language: JavaScript (javascript)

Checking for existence

When getting a value, you can check if the key exists using the second return value, for example:

lang, exists := languages["css"]
if exists {
    fmt.Println(lang)
} else {
    fmt.Println("Not found")
}Code language: JavaScript (javascript)

Output:

Not foundCode language: JavaScript (javascript)

In this example, if the css key exists in the languages map, the exists variable will be set to true or false otherwise.

Since the languages map does not have the css key, the output shows "Not found".

Deleting elements

To delete an element by a key, you use the built-in delete() function. For example, the following deletes the element with the key py in the languages map:

delete(languages,"py")Code language: JavaScript (javascript)

Iterating over elements of a map

To iterate over elements of a map, you use a for range loop. The first variable is the key and the second variable is the value. For example:

for key, value := range languages {
    fmt.Println(key, value)
}Code language: JavaScript (javascript)

Output:

go Go
js javascript
php PHPCode language: JavaScript (javascript)

If you use one loop variable, it will be the key:

package main

func main() {
    languages := map[string]string{
        "go": "Go",
        "js": "javascript",
        "py": "python",
    }

    for key := range languages {
        println(key)
    }
}Code language: JavaScript (javascript)

Output:

go
js
py

Putting it all together

package main

import "fmt"

func main() {
    // var languages = map[string]string{
    //     "go": "Go",
    //     "js": "javascript",
    //     "py": "python",
    // }

    languages := map[string]string{
        "go": "Go",
        "js": "javascript",
        "py": "python",
    }

    // add an element
    languages["php"] = "PHP"

    // checking existence
    lang, exists := languages["css"]
    if exists {
        fmt.Println(lang)
    } else {
        fmt.Println("Not found")
    }
    // delete an element
    delete(languages, "py")

    // iterate over map with range
    for key, value := range languages {
        fmt.Println(key, value)
    }
}Code language: JavaScript (javascript)

Summary

  • A map is a data structure that associates keys with values.
  • Use the make() function to create a map.
  • Use the for range loop to iterate over map elements.
Was this tutorial helpful?