In Golang a for loop is a way to loop through an iterable.

The most basic For Loop

1
2
3
4
i := 0
for i <= 3 {
  i = i + 1
}

A classic For Loop

1
2
3
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.

1
2
3
4
for {
  // do something
  break // kill the loop
}

When to use continue

A continue will move to the next iteration of the loop

1
2
3
4
5
6
7
for i := 0; i <= 5; i++ {
  if i%2 == 0 {
    // skip if even number
    continue // move to next iteration
  }
  // do something with odd number
}