Introduction
The if-else statement in Go lets you execute code conditionally based on a boolean expression. Go's version is deliberately minimal: no parentheses around the condition are required, but curly braces are always mandatory, even for single-statement bodies. This keeps code visually consistent across the entire language ecosystem, since gofmt enforces brace placement automatically.
Cricket analogy: Go's mandatory curly braces for if-else are like the strict, non-negotiable follow-through rule umpires enforce on every bowler's action, applied consistently even for a single simple delivery.
Syntax
if condition {
// executes when condition is true
} else if anotherCondition {
// executes when anotherCondition is true
} else {
// executes otherwise
}
// with an init statement
if result := compute(); result > 0 {
// result is scoped to this if-else chain only
}Explanation
The condition must evaluate to a boolean; Go does not allow implicit conversion from integers or other types like C does. The opening brace must be on the same line as the if, else if, or else keyword, otherwise the compiler inserts a semicolon automatically and breaks the chain. Go also supports an optional init statement before the condition, separated by a semicolon. Any variable declared there is scoped only to the if-else block, which is a handy way to keep helper variables out of the surrounding function scope.
Cricket analogy: Go disallowing implicit int-to-boolean conversion is like an umpire refusing to treat "three appeals" as automatically meaning "out"; the condition must explicitly be a genuine dismissal, not just any nonzero number of appeals.
Example
package main
import "fmt"
func classify(score int) string {
if score >= 90 {
return "A"
} else if score >= 75 {
return "B"
} else {
return "C"
}
}
func main() {
scores := []int{95, 80, 60}
for _, s := range scores {
if grade := classify(s); grade == "A" {
fmt.Println(s, "-> top grade:", grade)
} else {
fmt.Println(s, "-> grade:", grade)
}
}
}Output
95 -> top grade: A
80 -> grade: B
60 -> grade: CKey Takeaways
- Parentheses around the condition are optional; braces are always required.
- The opening brace must stay on the same line as if/else if/else.
- The condition must be a genuine boolean expression.
- An init statement can declare variables scoped to the whole if-else chain.
- else and else if must start on the same line as the preceding closing brace.
Practice what you learned
1. Which part of a Go if statement is mandatory?
2. What happens if you write the opening brace of an if statement on its own line?
3. What is the scope of a variable declared in an if statement's init clause?
4. Can a Go if condition be a non-boolean value like an integer?
5. Where must the else keyword appear relative to the preceding closing brace?
Was this page helpful?
You May Also Like
switch Statements in Go
Understand Go's switch statement, including implicit break, fallthrough, tagless switches, and type switches.
Operators in Go
Explore arithmetic, comparison, logical, and bitwise operators in Go, including the unique AND NOT operator.
for Loops in Go
Master Go's single looping construct, which covers classic for, while-style, infinite, and range-based loops.
Error Handling in Go
Learn Go's idiomatic error handling using the built-in error interface and explicit error return values instead of exceptions.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics