In this tutorial, We are going to learn about how to delete or remove element from map in go. We can do it by using delete() built-in function in go.
delete() built-in function deletes the element from map based on key.
Function prototype:
delete(map_name, key)
Input Parameters:
map_name: map name from where element to
be removed.
key: This is key to be removed from map
Example with code:
package main
import (
"fmt"
)
func main() {
day := make(map[string]int)
day["Sunday"] = 0
day["Monday"] = 1
day["Tuesday"] = 2
day["Wedenday"] = 3
fmt.Println("Before deleting elements: ")
for key, val := range day {
fmt.Println(key, val)
}
delete(day, "Monday")
fmt.Println("After deleting elements: ")
for key, val := range day {
fmt.Println(key, val)
}
}
Output:
Before deleting elements:
Sunday 0
Monday 1
Tuesday 2
Wedenday 3
After deleting elements:
Sunday 0
Tuesday 2
Wedenday 3
To learn more about golang, You can refer given below link:
https://techieindoor.com/go-lang-tutorial/
References:
https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/fmt/
https://golang.org/pkg/fmt/#Println