Home »
Golang »
Golang FAQ
Does Go language have ternary operator?
Learn, what is ternary operator, does Go language supports ternary operator?
Submitted by IncludeHelp, on October 04, 2021
Let's understand, what is the ternary operator?
The ternary operator is a part of the syntax for basic conditional expressions in several programming languages. It is commonly referred to as the conditional operator and requires three operands, a condition, true block, and false block.
Now, coming to the question, does Go language have/supports ternary operator?
The answer is - No. Go programming language does not have a ternary operator. You may use the following to achieve the same result:
if expr {
// true block
} else {
// false block
}
Example:
// Golang program to find
// largest number two numbers
package main
import "fmt"
func main() {
x := 10
y := 20
var largest int
if x > y {
largest = x
} else {
largest = y
}
fmt.Println("Largest number is:", largest)
}
Output:
Largest number is: 20
Golang FAQ »