Skip to content

How to Add and Remove From a Map in Golang

There are two ways to add an item to a map and one way to remove it from a map:

func delete(map, key)

Simple code example:

package main

import "fmt"

func main() {

	// Add to the map at the initialization moment
	userCity := map[string]string{
		"Morteza": "Utrecht",
	}

	// Add a new item to the map
	userCity["Masi"] = "Amsterdam"

	// Remove an item with key
	delete(userCity, "Morteza")

	fmt.Println(userCity)
}

Leave a Reply

Your email address will not be published. Required fields are marked *