Chapter 31: Go Conditions
Go Conditions” in beginner tutorials, documentation, YouTube videos, or W3Schools/GeeksforGeeks-style articles, they almost always mean:
How to write conditional logic — i.e. if, else if, else, and the very special if with initialization pattern that Go loves so much.
Go has no ternary operator (?:), no switch expression, and no do-while — so the if statement carries almost all decision-making work.
Today I’ll explain everything like we’re sitting together with VS Code open, writing small programs and running them live.
1. Basic if / else / else if – Syntax & Style
Go’s if is very clean:
- No mandatory parentheses around the condition
- Braces {} are always required (even for one statement)
- No semicolon after condition
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package main import "fmt" func main() { age := 17 hasPermit := true // Basic if if age >= 18 { fmt.Println("You are an adult") } // if + else if age >= 18 { fmt.Println("Adult") } else { fmt.Println("Not adult yet") } // if + else if + else if age >= 60 { fmt.Println("Senior citizen") } else if age >= 18 { fmt.Println("Adult") } else if age >= 13 { fmt.Println("Teenager") } else { fmt.Println("Child") } // Combining conditions with logical operators if age >= 18 && hasPermit { fmt.Println("Can drive with permit") } } |
Very important style rules in Go community (2025–2026):
- Always use braces {} — even for single line → if x > 0 { return x } — never omit braces
- Prefer early returns instead of deep nesting → “happy path” at left margin
2. The Famous Go “if with Initialization” Pattern
This is one of the most loved features of Go — you can declare a variable only visible inside the if scope.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Classic real-world example – error handling file, err := os.Open("config.json") if err != nil { fmt.Println("Failed to open file:", err) return } // file is usable here — err is not visible anymore // Another very common pattern if n := len(name); n == 0 { fmt.Println("Name is empty") } else if n < 3 { fmt.Println("Name too short") } else { fmt.Println("Name length is fine:", n) } // n and err are NOT visible here — they are scoped to the if block |
Why this pattern is so powerful:
- Variable lives only as long as needed
- Reduces variable scope pollution
- Very common idiom in Go: if err != nil { … }
3. Comparison & Logical Operators inside Conditions
You already know them, but here’s how they are used most often:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
temperature := 36.8 isRaining := false hasUmbrella := true if temperature >= 35 && !isRaining { fmt.Println("Very hot & dry – stay hydrated") } else if temperature >= 30 || isRaining { fmt.Println("Either hot or raining – be prepared") } else if hasUmbrella && isRaining { fmt.Println("You have umbrella → you can go out") } else { fmt.Println("Stay inside") } |
Short-circuit evaluation (very important safety feature):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
var ptr *int = nil // SAFE – thanks to short-circuit if ptr != nil && *ptr > 100 { fmt.Println("Big positive value") } else { fmt.Println("Safe – no panic") } // This would panic if ptr == nil // if *ptr > 100 { ... } |
4. Common Real-World Patterns You Will Use Every Day
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
// Pattern 1: Input validation username := "webliance" if len(username) == 0 { fmt.Println("Username required") } else if len(username) < 3 { fmt.Println("Username too short") } else if len(username) > 20 { fmt.Println("Username too long") } else { fmt.Println("Username accepted") } // Pattern 2: Early return (very idiomatic) func divide(a, b float64) (float64, error) { if b == 0 { return 0, fmt.Errorf("division by zero") } return a / b, nil } // Pattern 3: Nested if → better as early return if user != nil { if user.IsLoggedIn() { if user.HasPermission("admin") { // do admin thing } } } // Better style: if user == nil { return ErrNotLoggedIn } if !user.IsLoggedIn() { return ErrNotLoggedIn } if !user.HasPermission("admin") { return ErrNoPermission } // happy path here |
5. Quick Practice – Try Writing These
- Write an if that checks if a number is even and positive
- Write an if that prints different messages based on temperature:
- < 0 → “Freezing”
- 0–15 → “Cold”
- 16–25 → “Pleasant”
-
25 → “Hot”
- Write a safe check: if a map key exists and its value > 100, print “Big value”
Which pattern felt most natural to you?
Any part still confusing?
- Why no ternary operator in Go?
- How to avoid deep if nesting?
- Short-circuit evaluation with function calls that have side effects?
- Or ready to move to switch statement next?
Keep writing small if conditions — they control almost every meaningful decision in your future programs.
You’re doing really well — keep asking! 💪🇮🇳🚀
