Is it safe to delete elements from a map while iterating over it in Go?
Hello everyone! I just started learning Go and encountered unexpected behavior when working with a map. I need to iterate over all elements of a map and delete those that don't meet a certain condition. In other languages (e.g., Python), modifying a dictionary during iteration often leads to errors or undefined behavior. I wrote this code:
```go
package main
import "fmt"
func main() {
m := map[int]string{
1: "apple",
2: "banana",
3: "cherry",
4: "date",
}
for k, v := range m {
if v == "banana" || v == "date" {
delete(m, k)
}
}
fmt.Println(m)
}
```
The code works and outputs `map[1:apple 3:cherry]`. But I'm not sure if this is safe. Could it cause a panic, data race, or oth