Sobes.tech
Junior — Middle

How to iterate over elements in a Map structure in Go using syntactic constructs?

sobes.tech AI

Answer from AI

In the Go language, to iterate over the elements of a map, the for range construct is used. It allows iterating over the keys and values of the map. 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)
}

In this example, key and value sequentially take values from the map m. If only the key or only the value is needed, the unnecessary variable can be omitted using _.