Conditional Branching (if)
In Go, the
if statement doesn't require parentheses around the condition. It can also include a short "initialization" statement before the condition.go
Wait! Variables declared in the
if initialization are only in scope until the end of the if/else block.The Only Loop: for
Go has only one looping construct: the
for loop. It can be used in three different ways:- Standard:
for i := 0; i < 10; i++ { ... } - While-style:
for x < 100 { ... } - Infinite:
for { ... }
go
switch Statement
Go's
switch is much cleaner than in C or Java.- No
breakis needed; it stops after the first matching case automatically. - Cases can be expressions, not just constants.
- A
switchwith no expression is just a cleaner way to write a longif/elsechain.
go
The defer Keyword
Wait! This is one of Go's most unique features.
defer schedules a function call to be run immediately after the surrounding function returns. It is most commonly used for cleanup (closing files, unlocking mutexes).go
[!TIP] Deferred calls are pushed onto a stack. If you have multipledeferstatements, they will run in Last-In, First-Out (LIFO) order.
Summary
| Feature | Syntax | Description |
|---|---|---|
| if | if c { ... } | Standard conditional |
| init if | if x := 1; x < 5 | Conditional with local variable |
| for | for { ... } | The ONLY loop in Go |
| range | for k, v := range | Iterating through slices/maps/channels |
| switch | switch { ... } | Multi-way branching (no break needed) |
| defer | defer f() | Delayed execution until return |
| break | break | Exit a loop or switch |
| continue | continue | Skip to next loop iteration |
| fallthrough | fallthrough | Manually force next switch case to run |
| Logic | Clean, minimalist, and easy to read | |
| Safety | Prevents "Dangling else" and "forgotten break" bugs |