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.
Resources
Section titled “Resources”Exercises
Section titled “Exercises”range1 compile
Section titled “range1 compile”Iterate a slice with range.
Show hint
Use the `range` keyword to iterate over collections like arrays, slices, and maps.range2 compile
Section titled “range2 compile”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.range3 test
Section titled “range3 test”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.