In this shot, we will learn how to delete or remove an item from a map in Golang.
A map is an unordered collection of key-value pairs, where keys are unique.
We can remove an item from a map using the delete() function.
delete(map, key)
This function accepts map and key as parameters. It will delete the item for the given key in the given map.
It returns nothing, as this operation affects the original map.
In this example, we will try to delete a fruit item from the map fruits, where the key is the fruit name and the value is the number of fruits.
package main//import the format packageimport("fmt")//program execution starts herefunc main(){//declare and initialize a mapfruits := map[string]int{"orange":20,"apple":24,"guava":43,"watermelon":10,}//delete an itemdelete(fruits, "guava")//display the mapfmt.Println(fruits)}
In the above code snippet:
main() function in Golang.fruits, where keys are of the string type, and the values are of the int type.guava from the map fruits using the delete function.