GoLang Map - Update and Delete Items
In this tutorial, we will learn how to update and delete elements from a Map along with the working examples of it.
We can update values and delete items in Map based on the key in Go language.
Map will store the key-value pair type of data.
Map Updating Items
In GO language, we can update values using the key.
Syntax:
map[key]=value
Example 1:
Let us create a map named manager with 2 key-value pairs and update values.
package main
import ("fmt")
func main() {
// Create an empty map such that key stores string type values,
//value stores integer type values and add 2 key-value pairs.
var manager = map[string]int{"Sravan":23,"Anil":35}
// Display the map
fmt.Println("Actual map: ",manager)
// Update Sravan Age to 34
manager["Sravan"]=34
// Update Anil Age to 21
manager["Anil"]=21
// Display the map
fmt.Println("Final map: ",manager)
}
Output:
Actual map: map[Anil:35 Sravan:23]
Final map: map[Sravan:34 Anil:21]
Explanation for the above output:
We have updated value of Sravan to 34 and Anil to 21.
Example 2:
Let us create a map named fruits with 5 key-value pairs and update some of them.
package main
import ("fmt")
func main() {
// Create an empty map such that key and valuestores string type // values and add 5 key-value pairs.
var fruits = map[string]string{"apple":"guntur","guava":"nepal","banana":"guntur","grapes":"hyd","papayya":"hyd"}
// Display the map
fmt.Println("Actual Map: ",fruits)
// update value of apple to japan
fruits["apple"]="japan"
// update value of apple to usa
fruits["papayya"]="usa"
// Display the map
fmt.Println("Final Map: ",fruits)
}
Output:
Actual Map: map[guava:nepal banana:guntur grapes:hyd papayya:hyd apple:guntur]
Final Map: map[apple:japan guava:nepal banana:guntur grapes:hyd papayya:usa]
Explanation for the above output:
We have updated apple's value to "japan" and papayya's value to "usa".
Map Delete Items
We can delete the items by using the key.
Syntax:
delete(map_name,key)
Parameters:
It takes two parameters.
- map_name is the name of the map.
- key is the key present in the map to be deleted.
Example 1:
Let us create a map named manager with 2 key-value pairs and delete one of them.
package main
import (
"fmt"
)
func main() {
// Create an empty map such that key stores string type values,
//value stores integer type values and add 2 key-value pairs.
var manager = map[string]int{"Sravan": 23, "Anil": 35}
// Display the map
fmt.Println("Actual map: ", manager)
// Delete "Sravan"
delete(manager, "Sravan")
// Display the map
fmt.Println("Final map: ", manager)
}
Output:
Actual map: map[Anil:35 Sravan:23]
Final map: map[Anil:35]
Explanation for the above output:
We can see that Item-"Sravan" is deleted from the manager map.
Example 2:
Let us create a map named fruits with 5 key-value pairs and delete some of them.
package main
import (
"fmt"
)
func main() {
// Create an empty map such that key and valuestores string type // values and add 5 key-value pairs.
var fruits = map[string]string{"apple": "guntur", "guava": "nepal", "banana": "guntur", "grapes": "hyd", "papayya": "hyd"}
// Display the map
fmt.Println("Actual Map: ", fruits)
// delete apple
delete(fruits, "apple")
// delete guava
delete(fruits, "guava")
// Display the map
fmt.Println("Final Map: ", fruits)
}
Output:
Actual Map: map[apple:guntur banana:guntur grapes:hyd guava:nepal papayya:hyd]
Final Map: map[banana:guntur grapes:hyd papayya:hyd]
Explanation for the above output:
After removing the items "apple" and "guava", there are only 3 items in the fruits map.
Conclusion
Now we know how to update and delete values in a Golang Map data structure and understood the implementation with working examples.