Go - Map remove element example

In this example, we show how to delete an element in a Map in Golang with an example.

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

Go - Map remove element example

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]

Comments