remove a key and value from a map in go

To remove a key and value from a map in Go, you can use the delete function. The delete function takes two arguments: the map to delete from, and the key to delete. Here's an example:

main.go
myMap := map[string]int{"a": 1, "b": 2, "c": 3}

// delete the key/value with the key "b"
delete(myMap, "b")

fmt.Println(myMap) // output: map[a:1 c:3]
153 chars
7 lines

In this example, we first define a map with some key/value pairs. We then use delete to remove the key/value with the key "b". Finally, we print out the map to verify that the "b" key/value has been removed.

Note that if you try to delete a key that doesn't exist in the map, there will be no error, and no action will be taken.

related categories

gistlibby LogSnag