Learn to make decisions in your Go programs with if/else and switch statements
Learning Objectives
Write if, else if, and else statements
Use the switch statement for multiple conditions
Understand Go's unique if with initialization
Work with type switches
Making Decisions in Go
Conditional statements allow your program to make decisions and execute different code based on conditions. Go provides two main constructs: if statements and switch statements.
The if Statement
The basic if statement executes code when a condition is true:
main.go
1
2
3
4
5
6
7
8
9
10
11
package main
import"fmt"funcmain() {
age :=20if age >=18 {
fmt.Println("You are an adult")
}
}
No Parentheses Needed
Unlike C, Java, or JavaScript, Go does not require parentheses around the condition. However, braces {} are always required, even for single-line bodies.
if-else Statement
Add an else block for code that runs when the condition is false:
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
package main
import"fmt"funcmain() {
temperature :=15if temperature > 25 {
fmt.Println("It's hot outside")
} else {
fmt.Println("It's not too hot")
}
}
flowchart TD
A[Start] --> B{score >= 90?}
B -->|Yes| C[Grade: A]
B -->|No| D{score >= 80?}
D -->|Yes| E[Grade: B]
D -->|No| F{score >= 70?}
F -->|Yes| G[Grade: C]
F -->|No| H{score >= 60?}
H -->|Yes| I[Grade: D]
H -->|No| J[Grade: F]
if with Initialization
Go allows you to include a short statement before the condition. The variable is scoped to the if block:
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package main
import"fmt"funcmain() {
// Traditional way x :=10if x > 5 {
fmt.Println("x is greater than 5")
}
// With initialization - y only exists inside the if blockif y :=20; y > 15 {
fmt.Println("y is greater than 15")
}
// fmt.Println(y) // Error! y is not defined here}
When to Use
Initialization in if is particularly useful when working with functions that return a value and an error. You’ll see this pattern often in Go code.
package main
import"fmt"funcmain() {
num :=1switch num {
case1:
fmt.Println("One")
fallthroughcase2:
fmt.Println("Two (or fell through from One)")
fallthroughcase3:
fmt.Println("Three (or fell through)")
default:
fmt.Println("Default")
}
}
Use fallthrough Carefully
The fallthrough keyword is rarely used in Go. It unconditionally falls through to the next case regardless of that case’s condition. Consider if you really need it.
Type Switch
Go has a special type switch for checking the type of an interface value:
interface{} (or any in Go 1.18+) is the empty interface that can hold any type. We’ll cover interfaces in detail later in the course.
Nested Conditionals
You can nest if statements and switches:
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main
import"fmt"funcmain() {
age :=25 hasLicense :=trueif age >=18 {
if hasLicense {
fmt.Println("You can drive")
} else {
fmt.Println("Get a license first")
}
} else {
fmt.Println("Too young to drive")
}
}
Avoid Deep Nesting
Deeply nested conditionals are hard to read. Consider using early returns or restructuring your logic.
Better: Early Return Pattern
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package main
import"fmt"funccanDrive(age int, hasLicense bool) string {
if age < 18 {
return"Too young to drive" }
if !hasLicense {
return"Get a license first" }
return"You can drive"}
funcmain() {
fmt.Println(canDrive(25, true))
fmt.Println(canDrive(25, false))
fmt.Println(canDrive(16, true))
}
No Ternary Operator
Go Design Choice
Go does not have a ternary operator (condition ? value1 : value2). Use an if-else statement instead. This keeps Go code explicit and readable.
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package main
import"fmt"funcmain() {
age :=20// This won't work in Go:// status := age >= 18 ? "adult" : "minor"// Instead, use if-else:var status stringif age >=18 {
status = "adult" } else {
status = "minor" }
fmt.Println(status)
}
Summary
Today you learned:
Basic if, else if, and else statements
Go’s unique if with initialization syntax
The switch statement for cleaner multiple conditions
Switch without expression for range comparisons
Type switches for checking interface types
Why Go doesn’t have a ternary operator
Practice Exercise
Write a program that determines if a year is a leap year
Create a simple calculator that uses switch to handle different operations
Write a function that returns the name of a month given its number (1-12)
Implement a grade calculator using both if-else and switch
Next Steps
Tomorrow, we’ll explore loops - the only loop construct in Go: the versatile for loop!
Now, complete the quiz below to test your knowledge and unlock Day 5!
Quiz
Complete this quiz with a minimum score of 80% to mark Day 4 as complete.
Loading quiz...
Discussion
Have questions or want to discuss this lesson? Join the conversation below!
Settings
Appearance
Cloud Sync
Sync StatusChecking...
Link or Sign In to sync progress across devices
Data
This will delete all your progress for this course
Discussion
Have questions or want to discuss this lesson? Join the conversation below!