How does the range loop work in Go?

Answer

The range keyword iterates over arrays, slices, maps, strings, and channels. For slices and arrays it yields the index and value: for i, v := range slice { ... }. For maps it yields the key and value. For strings it yields the byte position and the Unicode code point (rune). For channels it receives values until the channel is closed. Discard either variable with the blank identifier: for _, v := range slice or for i := range slice. Range is idiomatic Go for any iteration over built-in collection types.