Range loop
A range loop is a Go for-loop form that iterates over values or entries produced by a supported rangeable expression.
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.
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.
Related concepts
- value copyproduces · A value copy is an independently copied element value produced by iteration rather than a writable reference to the original collection element.
- blank identifieruses · A blank identifier is a language-defined name or symbol that receives or matches a value without binding it to a usable variable.
- call by valueproduces · Call by value is a parameter-passing semantics in which a called operation receives its own copy of an argument’s value.
- arrayuses · An array is an indexed, ordered collection of elements arranged in one or more dimensions, with elements accessed by position.
- stringuses · A string is a programming-language value representing an ordered sequence of characters or character units as text.
- Mapuses · A map is an associative data structure that stores key-value pairs and supports retrieving values by key.
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.


