For

The Go for loop has three forms, only one of which hassemicolons:

  • for init; condition; post { } - a loop using the syntax borrowed from C;
  • for condition { } - a while loop, and;
  • for { } - an endless loop.

Short declarations make it easy to declare the index variable right in the loop.

  1. sum := 0
  2. for i := 0; i < 10; i++ {
  3. sum = sum + i
  4. }

Note that the variable i ceases to exist after the loop.