Control Flow in Go
Learn conditionals, switch statements, and loops in Go with practical Docker-ready examples covering if/else, switch, for loops, and loop control
Control flow determines the order in which your program’s statements run. It is how a program makes decisions and repeats work. Go takes a deliberately minimal approach here: where most languages offer several looping keywords, Go provides exactly one — for. This is consistent with Go’s design philosophy of being a small, learnable language.
As a procedural, imperative language, Go uses familiar structured control flow: if/else conditionals, switch statements, and for loops. But Go adds its own twists. There are no parentheses around conditions, braces are always required, the switch statement does not fall through by default, and there is deliberately no ternary operator (? :). Go’s authors decided clarity was worth a little extra typing.
In this tutorial you will learn how to branch with if/else, match values with switch, iterate with the versatile for loop, range over collections, and control loops with break and continue — including Go’s labeled loops.
Conditionals: if / else
Go’s if statement needs no parentheses around the condition, but the braces are mandatory. A useful feature is the optional initialization statement: you can declare a variable scoped to the if/else block before the condition.
Create a file named conditionals.go:
| |
The init-statement form (if score := 85; score >= 80) is idiomatic Go. It keeps short-lived variables tightly scoped, which is especially common when checking errors: if err := doSomething(); err != nil { ... }.
Switch Statements
Go’s switch is more powerful and safer than its C counterpart. Cases do not fall through automatically, so you rarely need break. A single case can match multiple values, and a switch with no condition becomes a clean replacement for a long if/else chain.
Create a file named switch.go:
| |
Because Go does not fall through by default, you avoid a whole class of bugs common in C and Java where a forgotten break causes accidental execution of the next case. When you genuinely want fall-through behavior, you opt in with the explicit fallthrough keyword.
Loops: the for Statement
Go has a single looping keyword: for. It covers every looping need through three forms — the classic three-component loop, a condition-only “while” form, and an infinite loop.
Create a file named loops.go:
| |
There is no while keyword in Go — for condition { } does the job. Likewise, for { } with no clauses is an infinite loop, which you exit with break. One keyword, three behaviors.
Ranging Over Collections and Loop Control
The range clause iterates over slices, arrays, maps, strings, and channels. Combined with break and continue — and Go’s labeled loops — it handles most real-world iteration.
Create a file named range_loops.go:
| |
Note: Go intentionally randomizes map iteration order, so the example above uses a single-key map to keep output predictable. Labeled statements (outer:) work with both break and continue, giving you precise control over nested loops without resorting to flag variables.
Running with Docker
You can run every example without installing Go locally by using the official golang:1.23 image.
| |
Expected Output
Running conditionals.go:
The weather is pleasant
Grade: B (score 85)
Running switch.go:
It's the weekend!
Good afternoon
One
Two
Running loops.go:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
n is now 5
n is now 2
n is now 1
Total reached 30
Running range_loops.go:
0: apple
1: banana
2: cherry
Alice is 30
Even: 2
Even: 4
Even: 6
Stopping at 2 x 3
Key Concepts
- No parentheses, mandatory braces — Conditions are written without surrounding
(), but{ }is always required, even for a single statement. ifwith an init statement —if x := f(); x > 0 { }scopes a variable to the conditional block; this is the idiomatic way to handle errors in Go.switchdoes not fall through — Cases break automatically. Use the explicitfallthroughkeyword only when you want the next case to run.- One loop keyword —
forcovers C-style loops, while-style loops (for cond {}), and infinite loops (for {}). There is nowhileordo-while. rangefor iteration — Iterate over slices, maps, strings, and channels; map order is randomized by design.breakandcontinuewith labels — Labeled loops let you break out of or continue an outer loop directly, avoiding flag variables.- No ternary operator — Go deliberately omits
? :; use a shortif/elseinstead, favoring readability over brevity.
Comments
Loading comments...
Leave a Comment