25% off

Use code FUNCMAIN at checkout for 25% off all premium courses.

Get started →

Interactive Go · Lesson

For Loops

Go keeps things simple: it has exactly one loop keyword, for, which covers every looping need. The classic three-part form has an init, a condition, and a post statement:

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

Drop the init and post and it behaves like a while:

for health > 0 {
    health -= 10
}

And for ... range walks over a slice or map (you’ll use this a lot soon):

for index, item := range backpack {
    fmt.Println(index, item)
}

Your Quest

Use a for loop to print the numbers 1 to 5, one per line.

Expected output:

1
2
3
4
5

Hint

for i := 1; i <= 5; i++ { fmt.Println(i) }.

Additional Scrolls of Wisdom