if and else
What you'll learn:
- choose which code runs with
if,else ifandelse; - declare a variable right inside an
ifand know where it is visible; - replace a long
else ifchain with aswitchstatement; - explain how
switchin Go differs fromswitchin C.

Picture a fork in the road with a signpost. It reads: if it's raining, go left to the bus stop; otherwise, go right through the park. You read the condition and take one road, and this time you don't walk the other. A program needs forks like this too: print one thing or another, calculate one way or another.
Statements in main run in order, from top to bottom. The if statement runs the block in curly braces only when the condition after if is true. The block after else runs when the condition is false:
package main
import "fmt"
func main() {
t := 25
if t < 0 {
fmt.Println("frost")
} else if t < 20 {
fmt.Println("cool")
} else {
fmt.Println("warm") // warm
}
}Let's walk through the program:
t := 25: the variabletis25.if t < 0: the condition is false, so the first branch is skipped.else if t < 20: this condition is false too.else: the last branch remains, and the program printswarm.
Three kinds of branches
Each block in curly braces is called a branch. The example has three, and each has its own role.
if
The if branch opens the fork. Its header has three parts:
- the keyword
if; - a condition, such as
t < 0; - a block in curly braces that runs when the condition is true.
If the condition is false, the block is skipped, and the program moves on. An if can also stand alone, without else: then nothing runs when the condition is false.
else if
An else if branch adds one more condition to the fork. It is checked only when every condition above it was false.
An else if chain checks the conditions from top to bottom, runs the first true branch and skips the rest. So the order of branches matters. The second branch is checked only when t >= 0, once the first condition is already false, and its condition needs no t >= 0 && prefix.
Here the fork differs from a real road: a traveler may pick the best-fitting sign. Go, however, reads the signs strictly from top to bottom and takes the first one that fits.
else
The else branch has no condition. It runs when every condition above it is false, and it always comes last. Without it, nothing runs in that case.
Curly braces
Parentheses around the condition are not needed but do not break the build. Curly braces around a branch are required, even for one statement. Code without them does not compile:
x := 5
if x > 0 fmt.Println("positive") // ✗syntax error: unexpected name fmt, expected {The compiler expected { right after the condition but found the name fmt.
Branches can be nested: a block of an if holds another if with its own braces. Since braces are required, you always see which if each else belongs to.
A condition of type bool
The condition must have type bool. These fit:
- comparisons like
x > 0; - their combinations with
&&,||and!; - a variable of type
bool.
Go does not turn a number into true or false, so a check for zero is written out: x != 0. For if x { with x of type int, the compiler reports non-boolean condition in if statement.
Common mistake
The else was moved to a new line after the closing brace:
x := 5
if x > 0 {
fmt.Println("positive")
}
else { // ✗
fmt.Println("not positive")
}syntax error: unexpected keyword else, expected }The compiler inserts a semicolon by itself at the end of a line that ends in }. So the if statement ends at that brace, and the else on the next line stands alone. Write } else { on one line. The same goes for else if: it goes on the line with the closing brace of the previous branch.
Key points
ifruns a branch when its condition is true, andelseruns when every condition is false.- An
else ifchain runs the first branch with a true condition and skips the rest. - A condition has type
bool: a number in its place does not compile. - Braces around a branch are required, and
} else {goes on one line.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.