liveBook · Manning

Range loop

A range loop is a Go for-loop form that iterates over values or entries produced by a supported rangeable expression.

How 3 Manning books explain it

100 Go Mistakes and How to Avoid Them

A range loop is a Go loop construct that iterates over elements of an array, slice, map, string, pointer to an array, or receiving channel.

How it is used
Uses range loops to expose mistakes involving copied values, one-time expression evaluation, pointers, and string iteration.
How deep it goes
Works through observable results and contextual fixes for slices, arrays, channels, pointers, maps, and strings.
Examples
Traces unchanged account balances, a growing slice, reassigned channels, an edited array, customer pointers, and the string `"hêllo"`.
What you can do after
Diagnose/debug with it when a loop changes copies, reads unexpected values, retains an original channel, or reports byte positions.

Read how 100 Go Mistakes and How to Avoid Them explains range loop →

Go in Action, Second Edition

A range loop is a Go for-loop form that iterates over the elements or entries of a supported collection and can provide each element's index and value.

How it is used
Uses range to process collections of variable length, including command-line filenames and race results.
How deep it goes
Explains assignment forms, copied values, string indices, and map order; defers channel behavior.
Examples
Demonstrates multiple-file word counting, Unicode character positions, race results, and map filtering.
What you can do after
Apply it via Go's range clause to traverse variable-length collections and choose indices when modifying array elements.

Read how Go in Action, Second Edition explains range loop →

Go by Example

A for-range loop is a Go loop construct that iterates over values produced by a rangeable expression, including supported iterator functions.

How it is used
Consumes iterator-produced values in synchronous APIs while hiding whether the producer uses slices, channels, or concurrent internals.
How deep it goes
Explains consumption, depletion, early termination, yield signaling, and nil handling, but stops short of general range syntax.
Examples
Uses a Results iterator and a Summarize function that aggregates request outcomes and transferred bytes.
What you can do after
Apply it via Go's for-range syntax to consume custom iterators and terminate processing early when appropriate.

Read how Go by Example explains range loop →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.