The for loop is one of the most commonly used statements to repeatedly execute some logic. In Go it consists of the for
keyword, a header and a code block that contains the body of the loop wrapped in curly brackets. The header consists of 3 components separated by semicolons ;
: init, condition and post.
for init; condition; post {
// loop body - code that is executed repeatedly as long as the condition is true
}
Note: Unlike other languages, there are no parentheses ()
surrounding the three components of the header. In fact, inserting such parenthesis is a compilation error. However, the braces { }
surrounding the loop body are always required.
The init component usually sets up a counter variable, the condition checks whether the loop should be continued or stopped and the post component usually increments the counter at the end of each repetition.
for i := 1; i < 10; i++ {
fmt.Println(i)
}
This loop will print the numbers from 1
to 9
(including 9
).
Defining the step is often done using an increment or decrement statement, as shown in the example above.
The init and post components of the header are optional:
var sum = 1
for sum < 1000 {
sum += sum
}
fmt.Println(sum)
// Output: 1024
By omitting the init and post component in a for loop like shown above, you create a while loop in Go.
There is no while
keyword.
This is an example of Go's principle that concepts should be orthogonal. Since there is already a concept to achieve the behavior of a while loop, namely the for loop, while
was not added as an additional concept.
Inside a loop body you can use the break
keyword to stop the execution of the loop entirely:
for n := 0; n <= 5; n++ {
if n == 3 {
break
}
fmt.Println(n)
}
// Output:
// 0
// 1
// 2
In contrast, the keyword continue
only stops the execution of the current iteration and continues with the next one:
for n := 0; n <= 5; n++ {
if n%2 == 0 {
continue
}
fmt.Println(n)
}
// Output:
// 1
// 3
// 5
The condition part of the loop header is also optional. In fact, you can write a loop with no header:
for {
// Endless loop...
}
This loop will only ever finish if the program exits or has a break
in its body.
When we use break
, Go will stop running the most inner loop. Similarly, when we use continue
, Go will run the next iteration of the most inner loop.
However, this is not always desirable. We can use labels together with break
and continue
to specifcy exactly from which loop we want to exit or continue, respectively.
In this example we are creating a label OuterLoop
, that will refer to the most outter loop. In the most inner loop, to say that we want exit from the most outter loop, we then use break
followed by the name of the label of the most outter loop:
OuterLoop:
for i := 0; i < 10; i++ {
for j := 0; j < 10; j++ {
// ...
break OuterLoop
}
}
Using labels with continue
would also work, in which case, Go would continue in the next iteration of the loop referenced by the label.
Go also has a goto
keyword that works in a similar way and allows us to jump to from a piece of code to another labeled piece of code.
Warning: Even though Go allows to jump to a piece of code marked with a label, using this feature of the language can easily make the code very hard to read. For this reason, using labels is often not recommended.