ADVERTISEMENT
ADVERTISEMENT

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 »

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.