Skip to content

Range

Beginner · Collections & Loops

for ... range iterates over arrays, slices, strings, maps, and channels. It yields index/value for slices and arrays, key/value for maps, and runes (with byte offsets) for strings. Use _ to ignore a part you don’t need.

Iterate a slice with range.

Show hint
Use the `range` keyword to iterate over collections like arrays, slices, and maps.

Source

Iterate a map’s keys and values with range.

Show hint
Use the `range` keyword to iterate over map values. Unlike arrays and slices, maps are iterated over by keys and values.

Source

Use range to collect the even numbers.

Show hint
Use a `for` loop with `range` to check if each number is even and append it to the `evenNumbers` slice.

Source