Go conditional statements let your program make decisions, like showing a discount only to members. In this post, you will learn if, else, else if and switch. Each idea comes with a short example that you can run yourself.
Every condition here uses the comparison and logical symbols from the last lesson. So if >= or && feels new, first read operators in Go with simple examples. You will also use the values from variables, constants and data types. And if Go is not set up yet, follow the guide to install Go and run your first program.
What Are Go Conditional Statements?
A conditional statement runs a block of code only when a condition is true. Here, a condition is any check that gives back true or false, such as age >= 18. A block is the code between curly braces { }.
Think of a traffic signal. If the light is green, you drive. Otherwise, you stop and wait. Go conditional statements work the same way, because they pick one path based on a simple yes or no answer.
The Basic if Statement
age := 20
if age >= 18 {
fmt.Println("You can vote")
}
// You can vote
First comes the word if, then the condition, and then the block. Notice that Go needs no brackets around the condition. However, the curly braces are always required, even for a single line.
Adding else and else if
Often you want a second path for when the condition is false. For that, add else. Then, to check more than two cases, add else if in between.
score := 72
if score >= 90 {
fmt.Println("Grade A")
} else if score >= 60 {
fmt.Println("Pass")
} else {
fmt.Println("Fail")
}
// Pass
Go checks each condition from top to bottom. As soon as one is true, it runs that block and skips the rest. So for a score of 72, the first check fails, the second one passes, and Go prints Pass. The diagram below shows this flow step by step.

The chain stops at the first true condition, and then the program continues.
Go Conditional Statements with a Short Statement
Go lets you run a small statement right before the condition, separated by a semicolon. As a result, the variable you create lives only inside the if and else blocks.
if n := len("Smita"); n > 3 {
fmt.Println("Long name with", n, "letters")
}
// Long name with 5 letters
Here n exists only inside this if. This keeps your code tidy, since the variable cannot leak into the rest of the function.
Checking Errors with if
You will see this pattern in almost every Go program. Many functions return a value and an error, so you check the error before you use the value.
num, err := strconv.Atoi("25a")
if err != nil {
fmt.Println("Not a valid number")
} else {
fmt.Println(num * 2)
}
// Not a valid number
The function strconv.Atoi turns text into a number. Because “25a” is not a number, err is not nil, which means something went wrong. We will cover errors in detail later in this series.
Switch: Cleaner Go Conditional Statements
A long chain of else if quickly becomes hard to read. In that case, a switch is cleaner. It compares one value against a list of cases. Think of a lift that stops only at the floor you pressed.
day := "Tuesday"
switch day {
case "Saturday", "Sunday":
fmt.Println("Weekend")
case "Monday":
fmt.Println("Start of the week")
default:
fmt.Println("Working day")
}
// Working day
The default case runs when no other case matches. Also, Go stops after the first matching case on its own. So unlike Java or C, you never need to write break.
Many Values in One Case
Look again at case "Saturday", "Sunday" above. A comma lets one case match several values. This saves you from repeating the same code twice.
Switch with No Condition
temp := 34
switch {
case temp >= 30:
fmt.Println("Hot")
case temp >= 20:
fmt.Println("Warm")
default:
fmt.Println("Cold")
}
// Hot
When you leave out the value after switch, each case becomes its own true or false check. In short, this is a neat way to write a long if else chain.
The fallthrough Keyword
level := 1
switch level {
case 1:
fmt.Println("Level 1 done")
fallthrough
case 2:
fmt.Println("Level 2 done")
case 3:
fmt.Println("Level 3 done")
}
// Level 1 done
// Level 2 done
The fallthrough keyword tells Go to also run the next case, without checking it. You will rarely need it, but now you know what it does.
If Else vs Switch: Which Go Conditional Statements to Use?
| Situation | Best choice |
|---|---|
| One or two simple checks | if or if else |
| Checking an error after a function call | if err != nil |
| One value with many possible matches | switch value |
| Many range checks, like grades or temperature | switch with no condition |
Both kinds of Go conditional statements give the same result, so pick the one that reads more clearly. As a simple rule, use switch once you have three or more branches.
Common Mistakes with Go Conditional Statements
| Error you see | Why it happens | How to fix it |
|---|---|---|
| syntax error: unexpected keyword else, expected } | The else sits on a new line. | Write } else { on the same line. |
| non-boolean condition in if statement | Here x is a number, not true or false. | Compare it, for example if x > 0. |
| syntax error: unexpected name fmt, expected { | The curly braces are missing. | Always wrap the block in { }. |
The first error surprises most beginners. Go adds an invisible semicolon at the end of a line that ends with }, so a lonely else breaks the code. Also, these small rules show up in interviews, so practise with my Go interview questions and answers.
Official Resources
These pages come straight from the Go team, so they are always accurate and up to date.
- A Tour of Go: If statements
- Go spec: If statements
- Go spec: Switch statements
- Effective Go: Control structures
Quick Recap
- Go conditional statements run code only when a condition is true.
- Use if, else if and else for a few checks, with no brackets but always with braces.
- A short statement like if n := len(s); n > 3 keeps the variable inside the if.
- Switch stops at the first match on its own, so you never write break.
- Keep } else { on one line to avoid the most common beginner error.
What’s Next?
Now your programs can make decisions. Next, we will make them repeat work with loops, so you can process a whole list in a few lines.
Coming soon: Go For Loops Explained with Examples


