Delete from a map

suggest change

The delete built-in function removes the element with the specified key from a map.

main.go

	people := map[string]int{"john": 30, "jane": 29}
	fmt.Printf("%v\n", people)

	delete(people, "john")
	fmt.Printf("%v\n", people)

Output:

map[jane:29 john:30]
map[jane:29]

If the map is nil or there is no such element, delete has no effect.

main.go

	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")

Output:

map[jane:29 john:30]
map[jane:29 john:30]

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents