Learn about How to delete an item from a map in Golang in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
How to delete an item from a map in Golang
// Golang program to delete an item from a map
package main
import "fmt"
func main() {
CountryCode := make(map[string]int)
CountryCode["ind"] = 101
CountryCode["aus"] = 102
CountryCode["eng"] = 103
CountryCode["rus"] = 104
CountryCode["usa"] = 105
delete(CountryCode, "rus")
fmt.Println("India :", CountryCode["ind"])
fmt.Println("Australia:", CountryCode["aus"])
fmt.Println("England :", CountryCode["eng"])
fmt.Println("Pakistan :", CountryCode["pak"])
fmt.Println("USA :", CountryCode["usa"])
}
In the main() function, we created a CountryCode map using make() function to store country code of specified country. Map store items in KEY/VALUE pair. Then we removed “rus” from CountryCode map using delete() function.
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
How to create a simple map in Golang
How to demonstrate the fmt.Sprintf() function in Golang