Timeline for What is the idiomatic Go equivalent of C's ternary operator?
Current License: CC BY-SA 4.0
20 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 26, 2025 at 15:43 | comment | added | Dan Jones |
"The compiler will inline such simple functions." If that's the case, I might as well just have a generic ternary function, and let the compiler inline it. func Tern[T any](test bool, a T, b T) { if test { return a } return b }
|
|
| Nov 30, 2024 at 15:20 | history | edited | informatik01 | CC BY-SA 4.0 |
Minor visual improvements for better readability to prevent rendering the `nt value = a <= b ? a : b` expression on two lines.
|
| May 26, 2022 at 11:22 | comment | added | Peter V. Mørch |
The problem with if/else is that I have to have a variable and I need to declare the type of it. I can't do callSomeFunc(if someCondition {34} else {42}). And then not worry about callSomeFunc's argument type.
|
|
| Jun 24, 2021 at 20:30 | history | notice added | Go Language | Recommended answer in Go Language | |
| Nov 3, 2020 at 18:12 | comment | added | C.W. | Downvoted because the solution is not equivalent to C ternary. The code block provided here cannot be used as an expression. An immediately evaluated closure does. | |
| Mar 4, 2019 at 23:36 | comment | added | KRK Owner | Dude. Clever? Yes. Takes time to figure out the intention if never seen before? Yes. Would I like to see that code at 3am if I'm called in an emergency? No. This is not clean and I don't advocate cleverness over the KISS principle. | |
| Dec 13, 2018 at 16:48 | comment | added | mattdlockyer | golang code like this reminds me of how nice the language is and how it mirrors ways I've written nodejs and JavaScript for years. Thanks for the clean answer! | |
| Dec 11, 2017 at 7:04 | comment | added | tom10271 | It is too long and looks complicated. I use ternary operator for simplicity | |
| Dec 2, 2017 at 12:33 | comment | added | Arnaldo Capo | Ok, all the benefits of golang and the fun it is to code. BUT NO ELVIS! Going back to JavaScript! lol, j/k | |
| Mar 28, 2017 at 16:32 | comment | added | Andrevinsky |
The map solution does not seem to work properly, as it calculates both branches, whereas the real ?: operator should calculate only the branch applicable for the condition.
|
|
| Oct 5, 2016 at 19:50 | comment | added | U Avalos | Yea I was gonna say the same thing @MaxMurphy. These golangers don't know what they're missing! The disadvantage to the current "idiomatic way" is that you can't define constants that way | |
| Aug 4, 2016 at 13:04 | comment | added | Max Murphy |
If if/else is the idiomatic approach then perhaps Golang could consider letting if/else clauses return a value: x = if a {1} else {0}. Go would be by no means the only language to work this way. A mainstream example is Scala. See: alvinalexander.com/scala/scala-ternary-operator-syntax
|
|
| Feb 28, 2015 at 12:39 | comment | added | Rick-777 |
c := (map[bool]int{true: a, false: a - 1})[a > b] is an example of obfuscation IMHO, even if it works.
|
|
| Jun 28, 2014 at 22:21 | comment | added | Thomas Ahle |
I guess you can discuss if if/else is more clear, but how is it shorter?
|
|
| Nov 14, 2013 at 20:03 | comment | added | nemo | @VladimirMatveev wrap the values in closures ;) | |
| Nov 14, 2013 at 18:13 | comment | added | Vladimir Matveev | @tomwilde your solution looks pretty interesting, but it lacks one of the main features of ternary operator - conditional evaluation. | |
| Nov 14, 2013 at 16:49 | comment | added | Brenden | @tomwilde interesting, but doesn't exactly flow off the tongue. Also, it would seem using a map instead of conditionals is less efficient. I will say your solution is creative though! | |
| Nov 14, 2013 at 14:48 | vote | accept | Fabien | ||
| Nov 14, 2013 at 14:35 | comment | added | thwd | Hey guys, look! I just ported the ternarity operator to the golangs! play.golang.org/p/ZgLwC_DHm0. So efficient! | |
| Nov 14, 2013 at 14:08 | history | answered | Gustavo Niemeyer | CC BY-SA 3.0 |