How to Use the Range Keyword in Go

The range keyword in Go iterates over collections like slices and maps, returning the index/key and value for each element.

The range keyword in Go iterates over elements in arrays, slices, maps, strings, or channels, providing the index (or key) and value for each iteration.

numbers := []int{10, 20, 30}
for i, v := range numbers {
    fmt.Printf("Index: %d, Value: %d\n", i, v)
}

For maps, range yields the key and value; for strings, it yields the byte index and the Unicode code point.