Python Control Flow: If with Init Statements & Switch
Go features clean conditional branching: if statements support short initialization statements (if x := compute(); x > 0) whose scope is confined to the if/else block. switch statements in Go break automatically without fallthrough and support multiple match expressions per case.
"An if initialization statement is renting a private locker for the duration of a swim session: the locker key exists while you are at the pool and vanishes automatically once you leave."
Deep Dive: How It Works
If Init Statement: if val, err := lookup(); err == nil confines temporary variables to local scope.
Auto-Break Switch: No manual break required; cases terminate automatically unless fallthrough is specified.
Tagless Switch: switch with no condition expression acts as a cleaner alternative to long if-else-if chains.
Syntax Blueprint
if err := validate(); err != nil {
return err
}
switch status {
case 200, 201:
fmt.Println("Success")
default:
fmt.Println("Error")
}Scoped initialization statement inside if condition, and multi-value switch matching.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Placing the opening brace { on a new line after if or func.Why it happens: Go parser automatically inserts semicolons at line endings.
How to fix: Opening braces must always remain on the same line: if x > 0 {
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Handle 404 Not Found Code
Set statusCode to 404.
Run the program to verify "HTTP Status: Not Found" is printed.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.