Looping in Go
Explore Go's unique approach to looping using a single for loop type that covers traditional loop patterns like for, while, and infinite loops. Understand loop control statements such as break and continue, and learn Go's syntax rules for loops to write clear and effective code.
We'll cover the following...
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:
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 happens at the start of each loop and must evaluate to true or the ...