Sobes.tech
Junior — Middle

How is the traversal of elements in a Map collection implemented?

sobes.tech AI

Answer from AI

In Go, to iterate over elements of a map, you use a for range loop. During each iteration, you can get the key and value from the map. The order of iteration is not guaranteed and can be different each time the program runs.

Example:

m := map[string]int{"a": 1, "b": 2, "c": 3}
for key, value := range m {
    fmt.Printf("Key: %s, Value: %d\n", key, value)
}

If you only need the key or only the value, you can ignore the other by using the blank identifier _.

How is the traversal of elements in a Map collection… - sobes.tech