Loops
JavaScript has four loop keywords: for, while, for...of, for...in. Go has exactly one — for — and reshapes it to cover every one of those cases. Recognising which "shape" of for is in use is the main new skill this chapter teaches.
Shape 1: The Classic Counting Loop
This looks almost identical to JavaScript's for (let i = 0; i < 5; i++) — minus the parentheses and semicolons-as-separators staying the same. i++ works exactly as before; Go also has i--, but notably no ++i prefix form — increment/decrement are statements in Go, not expressions, so they're always written after the variable.
Shape 2: for as a while Loop
Dropping the init and post clauses, leaving only a condition, turns for into the exact equivalent of JavaScript's while — there is no separate while keyword in Go at all, this is simply how it's written.
Shape 3: An Infinite Loop, with break
for with absolutely nothing after it loops forever, until something inside explicitly breaks out — the Go equivalent of JavaScript's while (true) { ... break; }. continue also works exactly as it does in JavaScript, skipping straight to the next iteration.
Shape 4: for...range — Iterating a Slice
[]string{...} is a slice — Go's closest equivalent to a JavaScript array, covered properly next chapter. for index, value := range fruits is the direct equivalent of JavaScript's for...of, except it always hands back both the index AND the value together — there's no separate index-only loop needed.
for _, fruit := range fruits { ... }. Go requires every declared variable to be used (Chapter 1), so _ exists specifically to say "I know this value exists, I'm deliberately not using it."
Looping Over a Map (Go's Object Equivalent)
The same range keyword also walks over a map (covered fully in Chapter 7) — Go's rough equivalent of a JavaScript object — handing back each key and value pair, the conceptual cousin of JavaScript's for...in.
range always visits elements 0, 1, 2... in order, ranging over a map visits entries in a deliberately randomised order every time the program runs. Code that depends on a specific order from a map will behave unpredictably — sort the keys first if order matters.
| JavaScript | Go |
|---|---|
| for (let i = 0; i < n; i++) | for i := 0; i < n; i++ |
| while (cond) | for cond |
| while (true) { ... break; } | for { ... break } |
| for (const item of array) | for _, item := range slice |
| for (const key in object) | for key, value := range map |
Coding Challenges
Write a classic counting for loop that prints every even number from 0 to 10 (inclusive), using i += 2 in the post clause instead of i++.
📄 View solutionWrite a for loop used as a while loop: starting with balance := 100, keep subtracting 30 and printing the new balance each time, stopping (using the condition, not break) once balance would go below 0.
📄 View solutionGiven colours := []string{"red", "green", "blue", "yellow"}, use for...range with the blank identifier _ to print just the values, one per line, with no index shown.
📄 View solutionChapter 4 Quick Reference
- for i := 0; i < n; i++ { } — classic counting loop, like JavaScript's for
- for condition { } — Go's while loop; there is no separate while keyword
- for { } — infinite loop; use break to exit, continue to skip an iteration
- for index, value := range slice { } — like for...of, but always gives both index and value
- for key, value := range map { } — like for...in, conceptually; order is NOT guaranteed
- _ (blank identifier) — discards a value Go would otherwise require you to use
- No ++i prefix form — i++ and i-- are statements, always written after the variable
- Next chapter: functions — multiple return values, named returns, and variadic parameters