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.
for i := 0; i < 3; i++ { fmt.Println(i) }
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 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, nodo ... while: usefor cond {}and anif ... { 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 usedapplies 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.modsays an earlier version, the old behavior (one shared variable) still applies. breakleaves the innermostfor,switchorselect.continuejumps 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; withrange, the slice header is evaluated once. - Loop conditions are re-evaluated each time:
for i := 0; i < len(s); i++callslenrepeatedly, which is cheap. - Floating-point counters accumulate error (
for x := 0.0; x != 1.0; x += 0.1may never end); iterate with an integer and compute the float. - Infinite loops are idiomatic for servers and worker goroutines: pair with
selectand actx.Done()case. - Performance: the compiler removes bounds checks for simple patterns; do not hand-optimize loops before profiling.