By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Go has exactly three control-flow statements: if, for and switch. There is no while, no do, no ternary operator. That is not a limitation you work around; it is a smaller surface to reason about. for covers every loop shape (bare, three-clause, range), if accepts an initialization statement, and switch does not fall through unless you say fallthrough.
Two rules save real bugs. First, range copies the value, so if you need to mutate an element, range over indices or take the address deliberately. Second, a switch on a value with no expression at the top — switch { case count > 10: ... } — is the idiomatic way to express classification rules without a chain of else if.
defer schedules a call for when the surrounding function returns, whether it returns normally or panics. Deferred calls run last-in-first-out, and their arguments are evaluated when the defer statement executes, not when the call runs. Use it for cleanup that must happen on every path: closing files, releasing locks, ending spans, rolling back transactions.
package main
import (
"fmt"
"strings"
)
type event struct {
Service string
Level string
}
func finish(name string) {
fmt.Println("finish", name)
}
func process(events []event) (count int) {
defer func() {
fmt.Printf("processed %d events\n", count)
}()
defer finish("process")
for _, e := range events {
if strings.EqualFold(e.Level, "error") {
count++
continue
}
switch e.Service {
case "api", "worker":
fmt.Println("counted", e.Service, e.Level)
default:
fmt.Println("skipped", e.Service, e.Level)
}
}
return count
}
func main() {
events := []event{
{Service: "api", Level: "error"},
{Service: "billing", Level: "info"},
{Service: "worker", Level: "warn"},
{Service: "worker", Level: "error"},
}
count := process(events)
fmt.Println("errors:", count)
}Experienced Go developers read control flow as a description of intent. A lookup with a fallback is an if with an initializer. A classification over many values is a switch. A loop that walks a collection is range. Cleanup belongs in defer, next to the acquisition it balances, so a reviewer can see the pair on one screen.
| Construct | Reach for it when | Watch out for |
|---|---|---|
if v, ok := m[k]; ok | A lookup decides the branch | The variable is scoped to the statement |
for i := 0; i < n; i++ | Indices or counting | Off-by-one errors; prefer range for collections |
for _, e := range events | Walking a slice or map | e is a copy — mutate via index if needed |
switch x { case ... } | Classifying one value | No fall-through; use fallthrough deliberately |
defer cleanup() | Every path must clean up | Arguments evaluate immediately; loop defers accumulate |
Key takeaways
Go has three loop and branch statements: if, for, switch.
range yields copies; mutate through an index when the element must change.
if and switch accept an initializer, which keeps scoped values out of the surrounding block.
defer runs cleanup for every return path and on panic, in last-in-first-out order.
Deferred arguments are evaluated at defer time; keep the defer next to the acquisition.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.