Introduction
Branching with if and else in Go is straightforward. Go’s if statements don’t require parentheses around conditions but do require braces around the code blocks.
The Code
// Branching with `if` and `else` in Go is
// straight-forward.
package main
import "fmt"
func main() {
// Here's a basic example.
if 7%2 == 0 {
fmt.Println("7 is even")
} else {
fmt.Println("7 is odd")
}
// You can have an `if` statement without an else.
if 8%4 == 0 {
fmt.Println("8 is divisible by 4")
}
// Logical operators like `&&` and `||` are often
// useful in conditions.
if 8%2 == 0 || 7%2 == 0 {
fmt.Println("either 8 or 7 are even")
}
// A statement can precede conditionals; any variables
// declared in this statement are available in the current
// and all subsequent branches.
if num := 9; num < 0 {
fmt.Println(num, "is negative")
} else if num < 10 {
fmt.Println(num, "has 1 digit")
} else {
fmt.Println(num, "has multiple digits")
}
}
// Note that you don't need parentheses around conditions
// in Go, but that the braces are required.
If/Else Variants
Basic If/Else
if 7%2 == 0 {
fmt.Println("7 is even")
} else {
fmt.Println("7 is odd")
}
The standard if/else structure. If the condition is true, the first block executes; otherwise, the else block executes.
If Without Else
if 8%4 == 0 {
fmt.Println("8 is divisible by 4")
}
The else clause is optional. Use this when you only need to take action if a condition is true.
Logical Operators in Conditions
if 8%2 == 0 || 7%2 == 0 {
fmt.Println("either 8 or 7 are even")
}
Combine multiple conditions using:
&& - AND operator (both conditions must be true)
|| - OR operator (at least one condition must be true)
! - NOT operator (negates a boolean)
If with Initialization Statement
if num := 9; num < 0 {
fmt.Println(num, "is negative")
} else if num < 10 {
fmt.Println(num, "has 1 digit")
} else {
fmt.Println(num, "has multiple digits")
}
Go allows you to declare variables as part of the if statement. The variable is scoped to the if/else blocks.
Variables declared in the if statement are only available within that if/else chain, not outside it.
The initialization statement is particularly useful for checking error values or doing a quick calculation before the condition check.
Common Patterns
Error Checking
if err := doSomething(); err != nil {
return err
}
Else If Chain
if x < 0 {
// negative case
} else if x == 0 {
// zero case
} else {
// positive case
}
Multiple Conditions
if isValid && isActive && !isDeleted {
// all conditions satisfied
}
Syntax Rules
Important syntax rules:
- No parentheses needed around conditions
- Braces
{} are always required, even for single statements
- The opening brace must be on the same line as the
if
Valid
if x > 0 {
fmt.Println("positive")
}
Invalid
// Error: missing braces
if x > 0
fmt.Println("positive")
// Error: opening brace on wrong line
if x > 0
{
fmt.Println("positive")
}
Key Takeaways
- No parentheses required around conditions
- Braces are always required, even for single-line blocks
- The
else clause is optional
- You can initialize variables in the if statement
- Variables declared in if statements are scoped to the if/else blocks
- Use
&&, ||, and ! for logical operations
- Opening braces must be on the same line as the if statement