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".
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?