To iterate over all key-value pairs in a map in Go, you can use a for range
loop. The for range
loop with a map iterates over each key-value pair in an arbitrary order.
Here is an example of iterating over all key-value pairs in a map:
myMap := map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
for key, value := range myMap {
fmt.Println("Key:", key, "Value:", value)
}
In this example, the for range
loop iterates over each key-value pair in the myMap
variable. The key
and value
variables are assigned to each key-value pair in each iteration. You can perform any desired operations on the key
and value
variables inside the loop.