HOWTO · Go
The Idiomatic Go Equivalent of C's Ternary Operator
This tutorial explores the idiomatic Go equivalent of C's ternary operator using if-else statements. Learn how to implement conditional logic in Go with clear examples and explanations. Discover the benefits of Go's design philosophy that emphasizes readability and maintainability, making your code easier to understand.
On this page
Go has no ternary operator. To choose a value from a condition, use an if/else statement and assign the result in each branch. This keeps the decision visible as control flow and works wherever a statement is allowed.
What C’s ternary operator does
C’s conditional operator uses the form condition ? true_expression : false_expression. It has three operands: a condition and two expressions that provide the result. C evaluates the condition, then evaluates the expression after ? when it is true or the expression after : when it is false. For example, int b = (a > 3) ? 10 : 20; assigns 10 to b when a is greater than 3, and 20 otherwise.
That form is an expression, so it can appear inside a larger expression or on the right side of an assignment. Go deliberately has no equivalent inline conditional expression. Its if statement is a statement, so a Go translation places the assignments in the branches instead of trying to put if where C expects an expression.
Use if-else in Go
The following complete program translates the C assignment and also demonstrates assigning 1 or 0 from a boolean condition. The short declaration a := 5 creates and initializes a; var b int declares b before either branch assigns it. The else branch must be attached to the closing brace of the if branch. Change a to see which value each decision assigns.
package main
import "fmt"
func main() {
a := 5
var b int
if a > 3 {
b = 10
} else {
b = 20
}
var isLarge int
if a > 3 {
isLarge = 1
} else {
isLarge = 0
}
fmt.Println(b)
fmt.Println(isLarge)
}
10
1
The first decision selects between 10 and 20, matching the C assignment. The second converts the condition into an integer flag. These are ordinary assignments in each branch. The output above shows the values printed by the program.
An if can also include an initialization statement, such as if n := len(items); n > 0 { ... }. The variable declared there is limited to the if statement and its else branches. Use this form when the value is needed only for that decision; otherwise, declare it before the statement so later code can use it.
Handle multiple conditions in Go
When a decision has more than two outcomes, an if/else if chain makes the order explicit. Go checks the conditions from top to bottom and runs the first matching branch; the final else handles all remaining values. Put the most useful distinctions in a clear order, especially when one condition includes values matched by another.
For several mutually exclusive values of one expression, a switch can be easier to scan than a long chain. A conventional switch compares one value with its case values. Go also permits an expressionless switch, where each case contains a boolean condition, similar to an if/else if chain. For example, switch { case score >= 90: grade = "A"; case score >= 80: grade = "B"; default: grade = "C" } expresses ordered ranges. In formatted Go source, place each case body on its own lines for readability.
Neither a switch nor an if-chain is a ternary expression: both are statements that control which statements run. Choose if/else for a small decision or ordered conditions, and choose switch when grouping several cases makes the choices easier to understand. A switch is an option, not a universally better replacement; the shape of the decision should guide the choice.
FAQ
What is the ternary operator in C?
It is C’s conditional operator, written condition ? true_expression : false_expression. The condition selects one of the two result expressions.
Does Go have a ternary operator?
No. Go uses if/else statements for conditional logic. You cannot use an if statement as an inline expression in an assignment or function argument.
Can I use nested if-else statements in Go?
Yes. For several outcomes, an else if chain is often easier to follow than nesting each if inside another. Both forms are statements, and each branch can assign a value or perform other work.
When should I use switch instead?
Use switch when the cases form a clear set of alternatives or when an expressionless switch makes several related conditions easier to read. Use an if/else if chain when the ordered comparisons communicate the decision more directly.
The practical translation is to keep the condition and its alternatives explicit: use C’s ?: when reading C code, and use Go’s if/else or switch statements for the corresponding Go control flow.