Go Loops

Count with the for statement.

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

Explanation

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

Explore the dry run
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