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
1
package main2
import "fmt"3
func 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
| Step / line | What happens | State after step | Output 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 |