...

/

Looping in Go

Looping in Go

Learn about looping in Go, its syntax, and uses.

Most languages have a few different types of loop statements: for, while, and do while. Go differs in that there is a single loop type, for, that can implement the functionality of all the loop types in other languages. In this lesson, we’ll discuss the for loop and its many uses.

C style

The most basic form of a loop is similar to C syntax:

Press + to interact
func main() {
for i := 0; i < 10; i++ {
fmt.Println(i)
}
}

This declares an i variable that is an integer scoped to live only for this loop statement. i := 0; is the loop initialization statement; it only happens once before the loop starts. i < 10; is the conditional statement; it ...