Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Keywords and Statements

The for statement

Reference for Go for: the three-part loop, while-style condition loop, infinite loop, range clause, break and continue, and per-iteration loop variables.

for is Go's only loop keyword. It covers every kind of looping through different shapes: a classic three-part loop, a condition-only loop that replaces while, an infinite loop, and the range form for walking collections (described on its own page, range).

Since Go 1.22 each iteration of a three-part for loop has its own copy of the variables it declares, so closures and goroutines started in the body see the value of that iteration.

Go
for i := 0; i < 3; i++ {
	fmt.Println(i)
}

Try it

Go The shapes of for

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 continue, labels and per-iteration variables

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 init; condition; post { ... }
for condition { ... }
for { ... }
for k, v := range x { ... }
Part Description
init Runs once before the first iteration. Variables declared with := are scoped to the loop.
condition A bool checked before each iteration. Missing means true.
post Runs after each iteration, before the next condition check. Must be a simple statement; i++ is a statement, not an expression.
range clause Iterates over a value, see range.

Errors and edge cases:

  • No while, no do ... while: use for cond {} and an if ... { break } at the end of an infinite loop.
  • ++ and -- are statements and cannot be used inside expressions; there is no prefix form.
  • The opening brace must be on the same line as the for.
  • declared and not used applies to loop variables.
  • Unused label: label outer defined and not used.

Usage notes

  • Per-iteration variables (Go 1.22+): the compiler copies the loop variable each iteration. In modules whose go.mod says an earlier version, the old behavior (one shared variable) still applies.
  • break leaves the innermost for, switch or select. continue jumps to the post statement. Both accept a label to target an outer loop.
  • Modifying the slice while looping over its index is safe if you re-check len; with range, the slice header is evaluated once.
  • Loop conditions are re-evaluated each time: for i := 0; i < len(s); i++ calls len repeatedly, which is cheap.
  • Floating-point counters accumulate error (for x := 0.0; x != 1.0; x += 0.1 may never end); iterate with an integer and compute the float.
  • Infinite loops are idiomatic for servers and worker goroutines: pair with select and a ctx.Done() case.
  • Performance: the compiler removes bounds checks for simple patterns; do not hand-optimize loops before profiling.