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 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.

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 →

What to know first

Related concepts

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.