Easy Tutorial
❮ Go Goto Statement Go Scope Rules ❯

Go Language Map (Collection)

A Map is an unordered collection of key-value pairs. The most important aspect of a Map is the ability to quickly retrieve data using a key, which is similar to an index pointing to the value of the data.

A Map is a collection, so we can iterate over it like we do with arrays and slices. However, Maps are unordered, and we cannot determine their return order because Maps are implemented using a hash table.

Defining a Map

A Map can be defined using the built-in function make or the map keyword:

/* Declare a variable, default map is nil */
var map_variable map[key_data_type]value_data_type

/* Use the make function */
map_variable := make(map[key_data_type]value_data_type)

If a map is not initialized, it creates a nil map. A nil map cannot be used to store key-value pairs.

Example

The following example demonstrates the creation and use of a Map:

package main

import "fmt"

func main() {
    var countryCapitalMap map[string]string /* Create a collection */
    countryCapitalMap = make(map[string]string)

    /* Insert key-value pairs, capitals of countries */
    countryCapitalMap["France"] = "Paris"
    countryCapitalMap["Italy"] = "Rome"
    countryCapitalMap["Japan"] = "Tokyo"
    countryCapitalMap["India"] = "New Delhi"

    /* Output map values using keys */
    for country := range countryCapitalMap {
        fmt.Println(country, "capital is", countryCapitalMap[country])
    }

    /* Check if an element exists in the collection */
    capital, ok := countryCapitalMap["American"] /* If true, it exists; otherwise, it does not */
    if ok {
        fmt.Println("American capital is", capital)
    } else {
        fmt.Println("American capital does not exist")
    }
}

The above example outputs:

France capital is Paris
Italy capital is Rome
Japan capital is Tokyo
India capital is New Delhi
American capital does not exist

delete() Function

The delete() function is used to remove an element from a collection, with parameters being the map and its corresponding key. Here is an example:

package main

import "fmt"

func main() {
    /* Create a map */
    countryCapitalMap := map[string]string{"France": "Paris", "Italy": "Rome", "Japan": "Tokyo", "India": "New Delhi"}

    fmt.Println("Original map")

    /* Print the map */
    for country := range countryCapitalMap {
        fmt.Println(country, "capital is", countryCapitalMap[country])
    }

    /* Delete an element */
    delete(countryCapitalMap, "France")
    fmt.Println("France entry deleted")

    fmt.Println("Map after deleting an element")

    /* Print the map */
    for country := range countryCapitalMap {
        fmt.Println(country, "capital is", countryCapitalMap[country])
    }
}

The above example outputs:

Original map
India capital is New Delhi
France capital is Paris
Italy capital is Rome
Japan capital is Tokyo
France entry deleted
Map after deleting an element
India capital is New Delhi
Italy capital is Rome
Japan capital is Tokyo

The capital of Japan is Tokyo The French entry has been deleted Map after deleting elements The capital of Italy is Rome The capital of Japan is Tokyo The capital of India is New Delhi

❮ Go Goto Statement Go Scope Rules ❯