The three parts of for
A for loop has init, condition, and post:
for init; condition; post {
// body
}
- init runs once before the loop
- condition is checked before each iteration
- post runs after each iteration
Any part can be omitted (but the semicolons stay).
range semantics
range is the idiomatic way to iterate:
for index, value := range collection {
// ...
}
The index is the position; the value is a copy for value types. For strings, range iterates by rune (Unicode code point), so multi-byte characters are handled correctly.