Mastering Go's if Statement: Syntax, Else‑if, and Common Pitfalls
This guide explains Go's if statement syntax, including required braces, optional else‑if and else clauses, the init‑statement form, and a crucial formatting rule that the else must appear on the same line as the closing brace to avoid automatic semicolon insertion.
In Go, the if statement is a conditional construct whose syntax requires braces even when the block contains a single statement:
if condition {
// code executed when condition is true
}If the condition evaluates to true, the code between { and } runs. Unlike languages such as C, Go always requires the braces.
The if statement can be followed by optional else if and else parts, and any number of else if clauses may appear. The first true condition's block is executed; if none are true, the else block runs.
if condition {
// code for true condition
} else if otherCondition {
// code for second condition
} else {
// fallback code
}Example: detecting whether a number is even or odd.
package main
import (
"fmt"
)
func main() {
num := 10
if num % 2 == 0 { // checks if number is even
fmt.Println("the number is even")
} else {
fmt.Println("the number is odd")
}
}The expression num % 2 == 0 checks the remainder of num divided by 2. If the remainder is zero, the number is even; otherwise, it is odd.
Go also supports an if form with an initialization statement that runs before the condition:
if statement; condition {
// code using variables declared in statement
}Rewriting the even‑odd check using this form:
package main
import (
"fmt"
)
func main() {
if num := 10; num % 2 == 0 { // num is scoped to the if‑else block
fmt.Println(num, "is even")
} else {
fmt.Println(num, "is odd")
}
}Another example demonstrates else if usage:
package main
import (
"fmt"
)
func main() {
num := 99
if num <= 50 {
fmt.Println("number is less than or equal to 50")
} else if num >= 51 && num <= 100 {
fmt.Println("number is between 51 and 100")
} else {
fmt.Println("number is greater than 100")
}
}When formatting if ‑ else statements, the else keyword must appear on the same line as the closing brace of the preceding block. Placing it on a new line causes the Go compiler to insert an automatic semicolon after the brace, resulting in a syntax error.
A Note
The correct layout is:
if condition {
// code
} else {
// code
}Incorrect layout (else on a new line) leads to an error like:
main.go:12:5: syntax error: unexpected else, expecting }Go automatically inserts a semicolon after a closing brace if it is the last token on the line. Therefore, the compiler sees:
if num%2 == 0 {
fmt.Println("the number is even")
}; // semicolon inserted by Go
else {
fmt.Println("the number is odd")
}To avoid this, keep the else on the same line as the closing brace, as shown in the corrected example above.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
