How do I delete a key from a map in Go?

In Go, you can delete a key from a map using the built-in `delete` function. This function takes two arguments: the map and the key you want to remove. If the key exists in the map, it will be removed; if the key does not exist, nothing happens.

Here’s a simple example to illustrate how to delete a key from a map:

package main import "fmt" func main() { // Create a map myMap := map[string]int{ "apple": 1, "banana": 2, "cherry": 3, } // Print the original map fmt.Println("Original map:", myMap) // Delete the key "banana" delete(myMap, "banana") // Print the modified map fmt.Println("Modified map:", myMap) }

In this example, after deleting the key "banana", the map will only contain the keys "apple" and "cherry".


keywords: Go delete map key Golang maps in Go