Conditional Statements
Learn Go's if/else statements, the if-with-a-short-statement pattern, and why Go has no ternary operator.
Introduction
Every non-trivial program needs to make decisions — running different code depending on some condition. Go's conditional statements will look familiar if you've used C-family languages, with a couple of distinctive Go touches: no required parentheses around the condition, and a handy short-statement form.
Basic if Statements
An `if` statement runs a block of code only when its condition evaluates to `true`. Unlike C or Java, Go does not require (or even allow, idiomatically) parentheses around the condition, but curly braces are mandatory, even for a single statement.
package main
import "fmt"
func main() { temperature := 35
if temperature > 30 { fmt.Println("It's hot outside.") }}Click Run to see what this code prints.
if / else
Add an `else` block to run alternate code when the condition is false.
package main
import "fmt"
func main() { age := 16
if age >= 18 { fmt.Println("You can vote.") } else { fmt.Println("You cannot vote yet.") }}Click Run to see what this code prints.
else if Chains
Chain multiple conditions together with `else if` to test several possibilities in order — Go stops at the first condition that evaluates to true.
package main
import "fmt"
func main() { score := 82
if score >= 90 { fmt.Println("Grade: A") } else if score >= 80 { fmt.Println("Grade: B") } else if score >= 70 { fmt.Println("Grade: C") } else { fmt.Println("Grade: F") }}Click Run to see what this code prints.
if With a Short Statement
Go lets you run a short initialization statement directly inside an `if`, separated from the condition by a semicolon. Any variable declared this way is scoped only to the if/else block — it doesn't leak into the surrounding code. This pattern is extremely common when working with functions that return a value alongside an error.
package main
import ( "fmt" "strconv")
func main() { input := "42"
if value, err := strconv.Atoi(input); err == nil { fmt.Println("Parsed value:", value) } else { fmt.Println("Failed to parse:", err) } // value and err are not accessible here — their scope ended with the if/else.}Click Run to see what this code prints.
Since many Go functions return a (result, error) pair, the if-with-short-statement pattern keeps the temporary result and error tightly scoped to exactly where they're needed, instead of leaking extra variables into the rest of the function.
Why Go Has No Ternary Operator
Languages like JavaScript or C let you write `condition ? valueIfTrue : valueIfFalse` as a compact one-liner. Go deliberately has no such operator. The language's designers felt ternary expressions often hurt readability, especially when nested, and preferred that developers write out a full if/else instead — even though it takes a couple more lines.
package main
import "fmt"
func main() { age := 20
// No ternary in Go — write it out explicitly instead: var status string if age >= 18 { status = "adult" } else { status = "minor" }
fmt.Println(status)}Click Run to see what this code prints.
Nesting Conditionals
You can nest if statements inside one another for more complex logic, though deeply nested conditionals are usually a sign the logic could be restructured — for example, by returning early from a function instead.
package main
import "fmt"
func classify(age int, hasTicket bool) string { if age >= 18 { if hasTicket { return "Admitted" } return "Needs a ticket" } return "Not old enough"}
func main() { fmt.Println(classify(25, true)) fmt.Println(classify(25, false)) fmt.Println(classify(15, true))}Click Run to see what this code prints.
Common Mistakes
- Wrapping the condition in parentheses out of habit from other languages — Go doesn't require them, and gofmt will flag unnecessary ones.
- Forgetting curly braces around a single-statement if body — unlike some languages, Go always requires them.
- Trying to access a variable declared in an if-short-statement outside the if/else block — its scope ends there.
- Writing deeply nested if/else chains instead of using early returns to flatten logic.
Best Practices
- Use the if-short-statement form when checking a function's (result, error) pair — it's idiomatic Go.
- Prefer early returns over deeply nested if/else blocks for readability.
- Keep conditions simple and readable; break complex boolean logic into well-named helper variables if needed.
- Let gofmt handle spacing and brace placement automatically rather than fighting its formatting.
Frequently Asked Questions
No, and idiomatic Go style (enforced by gofmt) omits them entirely. Curly braces around the body, however, are always required, even for single-line bodies.
Not directly — Go has no ternary operator. Use a full if/else statement, or in simple cases some developers use a small helper function, but this is less common.
It is scoped only to that if statement and its else/else-if branches — it does not exist outside the block.
Key Takeaways
- Go's if statements need no parentheses around the condition but always require curly braces.
- else if chains test multiple conditions in order, stopping at the first true one.
- The if-with-short-statement pattern (if x := f(); x > 0) is idiomatic and scopes temporary variables tightly.
- Go has no ternary operator by design — use a full if/else instead.
Summary
You now know how to branch your program's logic using if, else, and else if, including Go's distinctive short-statement pattern. Next, you'll learn about Go's single, versatile for loop, which covers every looping style you need.