Delete from a map
suggest changeThe delete
built-in function removes the element with the specified key from a map.
people := map[string]int{"john": 30, "jane": 29}
fmt.Printf("%v\n", people)
delete(people, "john")
fmt.Printf("%v\n", people)
map[jane:29 john:30]
map[jane:29]
If the map
is nil
or there is no such element, delete
has no effect.
people := map[string]int{"john": 30, "jane": 29}
fmt.Printf("%v\n", people)
// deleting a key that doesn't exist is a no-op
delete(people, "notfound")
fmt.Printf("%v\n", people)
// deleting a key in nil map is a no-op
var something map[string]int
delete(something, "notfound")
map[jane:29 john:30]
map[jane:29 john:30]
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents