m.Range
is a method in the Go programming language that is used to iterate over all key-value pairs stored in a map. It takes as input a function that will be called for each key-value pair in the map.
The syntax for using m.Range
is:
func (m mapType) Range(f func(key keyType, value valueType) bool)
Here, mapType
represents the type of the map, keyType
and valueType
represent the types of the keys and values in the map, respectively. The function f
takes two arguments: a key and a value, and returns a boolean value indicating whether the iteration should continue or not.
Example:
package main
import (
"fmt"
)
func main() {
m := make(map[string]int)
m["foo"] = 1
m["bar"] = 2
// Iterate over the map
m.Range(func(key string, value int) bool {
fmt.Printf("Key: %s, Value: %d\n", key, value)
return true
})
}
Output:
Key: foo, Value: 1
Key: bar, Value: 2