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 common iteration and mutation mistakes in Go.
- How deep it goes
- Traces value copies and expression evaluation across slices, arrays, channels, and strings, then contrasts contextual fixes.
- Examples
- Uses account balance updates, appended slices, channel reassignment, customer pointers, and a UTF-8 string to demonstrate pitfalls.
- What you can do after
- Diagnose and fix range-loop mistakes involving copied values, iteration inputs, pointers, and string indices.
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 varying size without manually checking each iteration's bounds.
- How deep it goes
- Explains iteration forms, value copies, string indices, map ordering and deletion, while deferring concurrent map access safety.
- Examples
- Uses multi-file word counting, race results, Unicode characters, and map filtering to demonstrate iteration.
- What you can do after
- Apply range loops in Go to process collections, update array elements by index, and filter maps.
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.
What to know first
- slicingSlicing selects or partitions a portion of an indexed sequence or multidimensional array, often along specified indices or dimensions.
- goroutineA goroutine is a lightweight function execution unit managed by the Go runtime that runs concurrently with other program activity.
- MapA map is an associative data structure that stores key-value associations and supports retrieving values by key.
- arrayAn array is an ordered collection of elements organized in one or more dimensions and accessible by position or index.
Related concepts
- pass by valueproduces · Pass by value is a parameter-passing semantics in which a called operation receives a copy of an argument's value.
- 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.
- value copyproduces · A value copy is an independently copied element value produced by iteration rather than a writable reference to the original collection element.
- Mapuses · A map is an associative data structure that stores key-value associations and supports retrieving values by key.
- stringuses · A string is a programming-language value representing textual data as an ordered sequence of characters or encoded units.
- arrayuses · An array is an ordered collection of elements organized in one or more dimensions and accessible by position or index.
Questions about range loop
What is range loop?
A range loop is a Go for-loop form that iterates over values or entries produced by a supported rangeable expression.
Which Manning books explain range loop?
100 Go Mistakes and How to Avoid Them, Go in Action, Second Edition and Go by Example.
What should I know before learning range loop?
Manning books that teach range loop usually introduce slicing, goroutine, Map and array first.
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.


