Go Reference ยท Keywords and Statements
The range clause
Reference for Go range: iterating arrays, slices, strings, maps, channels, integers and iterator functions, with what each form yields and common pitfalls.
range is the clause that makes a for loop walk over a collection. What you get per iteration depends on the type being ranged over: index and element for slices, key and value for maps, byte offset and rune for strings, values for channels, and a counter for integers. Since Go 1.23 it also accepts iterator functions.
You can drop the parts you do not need: for i := range s gives only indexes, for _, v := range s only values, and for range s just repeats len(s) times.
for i, v := range []string{"a", "b"} { fmt.Println(i, v) }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
for k, v := range x { ... } for k := range x { ... } for range x { ... } for k, v = range x { ... } // assign to existing variables
x type |
First value | Second value |
|---|---|---|
| array, pointer to array, slice | index int |
element (copy) |
| string | byte offset int |
rune (rune); invalid UTF-8 yields U+FFFD, advancing one byte |
| map | key | value |
| channel | element (only one value allowed) | none |
integer n (Go 1.22+) |
0 to n-1, of n's type |
none |
func(yield func() bool) |
none | none |
func(yield func(K) bool) |
K |
none |
func(yield func(K, V) bool) |
K |
V |
Errors and edge cases:
- A
nilslice or map yields nothing. Anilchannel blocks forever. - Ranging a channel ends only when it is closed and drained; forgetting to close it is a goroutine leak or deadlock.
rangeover a pointer to a slice or map is a compile error; dereference first. A pointer to an array is allowed.rangeover a function type that does not match a valid iterator shape:cannot range over f (variable of type ...).
Usage notes
- The element is a copy. To change elements, index into the original or range over a slice of pointers.
- Evaluation: the range expression is evaluated once. For slices the length is fixed at the start (appended elements are not visited); for arrays, ranging with the value makes a copy of the whole array unless you use a pointer or only the index.
- Maps: iteration order is deliberately unspecified and differs between runs. Deleting entries during iteration is safe; entries added during iteration may or may not be visited.
- Strings are walked by rune, not by byte;
len(s)counts bytes. Uses[i]in a plain loop for bytes. - Iterators:
maps.Keys,maps.Values,slices.All,slices.Valuesandslices.Sortedreturn or consumeiter.Seq/iter.Seq2values (Go 1.23+). Breaking out of the loop makesyieldreturnfalse. - Per-iteration variables (Go 1.22+) mean closures capture each iteration's
kandvseparately.