Skip to content
Tutorials Center
My learning

Topics

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.

Go
for i, v := range []string{"a", "b"} {
	fmt.Println(i, v)
}

Try it

Go What range yields for each type

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.

Go Copies, iterator functions and pitfalls

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

Go
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 nil slice or map yields nothing. A nil channel blocks forever.
  • Ranging a channel ends only when it is closed and drained; forgetting to close it is a goroutine leak or deadlock.
  • range over a pointer to a slice or map is a compile error; dereference first. A pointer to an array is allowed.
  • range over 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. Use s[i] in a plain loop for bytes.
  • Iterators: maps.Keys, maps.Values, slices.All, slices.Values and slices.Sorted return or consume iter.Seq/iter.Seq2 values (Go 1.23+). Breaking out of the loop makes yield return false.
  • Per-iteration variables (Go 1.22+) mean closures capture each iteration's k and v separately.