Go · Course navigation

Go Loops

Count with the for statement.

On this page

The concept

Go uses for for repetition. The counter starts at one, the condition is checked before each pass, and i++ advances it.

Code example

Go
package main
import "fmt"
func main() {
  for i := 1; i <= 3; i++ {
    fmt.Println(i)
  }
}
Output
1
2
3

Follow the code

Open the walkthrough to inspect the example one step at a time.

Show guided walkthrough
Guided walkthroughReady
1package main
2import "fmt"
3func main() {
4 for i := 1; i <= 3; i++ {
5 fmt.Println(i)
6 }
7}

Follow the example

Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.

View the complete dry-run table
Prepared trace of the published program. Some rows group repeated iterations.
Step / lineWhat happensState after stepOutput so far
Initialize i to 1 and check i <= 3.—
—
Print each value, advancing i after every pass.i = 1 → 2 → 3 → 4
1
2
3

Behind the scenes

How Go processes this code

The Go compiler builds an executable. Its runtime starts the main function in package main.