Golang Maps

In this tutorial, we will learn how to work with Maps in Golang with examples.

A map is an unordered collection of key/value pairs, where each key is unique.

Table of contents

  • Go - Simple Map Example
  • Go - Map loop example
  • Go - Map remove element example

Go - Simple Map Example

Maps are created with make() function or with map literals.

The below example shows how to create a Map, print Map, get value from Map:

package main

import "fmt"

func main() {

    // To create an empty map, use the builtin make function
    m := make(map[string]int)

    // Set key/value pairs using typical name[key] = val syntax.
    m["k1"] = 1
    m["k2"] = 3
    m["k3"] = 5
    m["k4"] = 7
    m["k5"] = 9

    fmt.Println("printing map:", m)

    // Get a value for a key with name[key].
    v1 := m["k1"]
    fmt.Println("v1: ", v1)

    v5 := m["k5"]
    fmt.Println("v5: ", v5)

    // The builtin len returns the number of key/value pairs when called on a map.
    fmt.Println("len:", len(m))

    delete(m, "k2")
    fmt.Println("map:", m)

    _, prs := m["k2"]
    fmt.Println("prs:", prs)

    // You can also declare and initialize a new map in the same line with this syntax.
    n := map[string]int{"foo": 1, "bar": 2}
    fmt.Println("map:", n)
}

Output:

printing map: map[k1:1 k2:3 k3:5 k4:7 k5:9]
v1:  1
v5:  9
len: 5
map: map[k1:1 k3:5 k4:7 k5:9]
prs: false
map: map[bar:2 foo:1]

Go - Map loop example

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

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

package main

import "fmt"

func main() {

    countries := map[string]string{
        "IN": "India",
        "NP": "Nepal",
        "TR": "Turkey",
        "JP": "Japan",
        "ZW": "Zimbabwe",
    }

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

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

Output:

IN => India
NP => Nepal
TR => Turkey
JP => Japan
ZW => Zimbabwe
countries[JP] = Japan
countries[ZW] = Zimbabwe
countries[IN] = India
countries[NP] = Nepal
countries[TR] = Turkey

In the first case, we loop by pair objects:

package main

import "fmt"

func main() {

    countries := map[string]string{
        "IN": "India",
        "NP": "Nepal",
        "TR": "Turkey",
        "JP": "Japan",
        "ZW": "Zimbabwe",
    }

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

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

package main

import "fmt"

func main() {

    countries := map[string]string{
        "IN": "India",
        "NP": "Nepal",
        "TR": "Turkey",
        "JP": "Japan",
        "ZW": "Zimbabwe",
    }

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

Go - Map remove element example

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

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

package main

import "fmt"

func main() {

    countries := map[string]string{
        "IN": "India",
        "NP": "Nepal",
        "TR": "Turkey",
        "JP": "Japan",
        "ZW": "Zimbabwe",
    }

    fmt.Println(countries)

    delete(countries, "NP")

    fmt.Println(countries)
}

Output:

map[IN:India JP:Japan NP:Nepal TR:Turkey ZW:Zimbabwe]
map[IN:India JP:Japan TR:Turkey ZW:Zimbabwe]

Golang Related Tutorials

Comments