To delete a key-value pair from a map in Go, you can use the delete()
function. Here's an example:
package main
import "fmt"
func main() {
m := map[string]int{
"apple": 5,
"banana": 10,
"orange": 15,
}
fmt.Println("Before deletion:", m)
delete(m, "banana")
fmt.Println("After deletion:", m)
}
In this example, we have a map m
with three key-value pairs. We want to delete the key-value pair with the key "banana". We achieve this by calling the delete()
function and passing the map (m
) and the key ("banana") as arguments.
Output:
Before deletion: map[apple:5 banana:10 orange:15]
After deletion: map[apple:5 orange:15]
The key-value pair with the key "banana" is deleted from the map.