if Statements and Init Expressions
Conditionals with optional init statements
if Statements and Init Expressions is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
if in Go
Go's if statement evaluates a boolean expression — no parentheses required, but braces are mandatory:
if x > 0 {
// positive
}else and else if
Chain conditions with else if and handle the fallback with else:
func classify(n int) string {
if n < 0 {
return "negative"
} else if n == 0 {
return "zero"
} else {
return "positive"
}
}if with Init Statement
Go allows an optional init statement before the condition — perfect for scoping temporary variables:
if err := doSomething(); err != nil {
// err is only in scope here
return err
}Scoping with Init Statement
Variables declared in the init statement are scoped to the if/else block. This prevents variable leakage into the surrounding function:
func process(id int) error {
if val, err := lookup(id); err != nil {
return err
} else {
use(val) // val is in scope here
}
return nil
}Boolean Expressions
Go's condition must be a bool — no implicit conversion:
- Valid:
if count != 0 - Invalid:
if count— compile error in Go (unlike C/JavaScript) - This prevents common off-by-one and nil-dereference bugs
Nil Checks
Check for nil pointers, slices, maps, and errors with == nil:
func greet(name *string) string {
if name == nil {
return "Hello, stranger"
}
return "Hello, " + *name
}Early Return Pattern
Go strongly favors early returns to reduce nesting (the "happy path" stays at the left margin):
func save(user User) error {
if user.Name == "" {
return errors.New("name required")
}
if user.Age < 0 {
return errors.New("invalid age")
}
return db.Insert(user)
}Combining Conditions
Use && and || to combine conditions. Short-circuit evaluation applies — the right side is only evaluated if needed:
if user != nil && user.IsActive() {
sendEmail(user)
}if vs switch
When you have many conditions on the same variable, switch is usually cleaner. Use if when:
- Conditions are independent boolean expressions
- You need init statements with error checks
- You have only 1–2 branches
Avoiding if-else Pyramids
Deeply nested if-else blocks hurt readability. Combat this with:
- Early returns
switchstatements for multiple cases- Breaking logic into smaller functions
- Guard clauses at the top of a function
Quick Check
What is unique about Go's if statement compared to C?
Recap: if Statements
Go's if statement is powerful and clean:
- No parentheses around the condition — braces required
- Init statement scopes variables to the if/else block
- Condition must be strictly
bool - Favor early returns over deep nesting
- Use
&&/||with short-circuit evaluation
Next: Go's versatile for loop.
Frequently asked questions
Is the “if Statements and Init Expressions” lesson free?
Yes — the full text of “if Statements and Init Expressions” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “if Statements and Init Expressions”?
Conditionals with optional init statements You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “if Statements and Init Expressions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- if Statements and Init Expressions
- The for Loop: All Patterns
- switch Statements in Go
- break, continue and Labels