August 2019
Beginner to intermediate
798 pages
17h 2m
English
As discussed earlier, Go does not offer the while keyword for writing while loops but allows you to use a for loop instead of a while loop. This section will present two examples where a for loop does the job of a while loop.
Firstly, let's look at a typical case where you might want to write something like while(true):
for {
}
It is the job of the developer to use the break keyword to exit this for loop.
However, the for loop can also emulate a do...while loop, which can be found in other programming languages.
As an example, the following Go code is equivalent to a do...while(anExpression) loop:
for ok := true; ok; ok = anExpression {
}
As soon as the ok variable has the false value, the for loop will terminate.
There is ...
Read now
Unlock full access