In Golang a for loop
is a way to loop through an iterable.
The most basic For Loop
i := 0
for i <= 3 {
i = i + 1
}
A classic For Loop
for i := 7; i <= 9; i++ {
// do something
}
For Loop without Conditions
A for
without a condition will loop forever, until either a break
or return
is hit.
for {
// do something
break // kill the loop
}
When to use continue
A continue
will move to the next iteration of the loop
for i := 0; i <= 5; i++ {
if i%2 == 0 {
// skip if even number
continue // move to next iteration
}
// do something with odd number
}