Introduction: Ternary Operator — Friend or Foe?
\n\nIf you came to Go from the world of C, C++, Java, or JavaScript, you are probably used to the concise construct:
\n\nint max = (a > b) ? a : b;\n\nThis is the ternary operator. It is compact, elegant, and allows you to write a simple conditional in one line. But when you start writing in Go, a surprise awaits you: Go does not have a ternary operator. At all. No ? :.
Why did the language creators take this step? How do you write short conditional assignments now? And most importantly, how do you do it idiomatically, that is, in the style accepted by the Go community?
\n\nIn this article, we will cover:
\n- \n
- Why Go doesn't have a ternary operator (spoiler: it's a deliberate decision). \n
- How to replace the ternary with
if-else. \n - Advanced techniques: functions, maps, inlining. \n
- When you can (and should) break the rule. \n
Let's go!
\n\nWhy Did Go Abandon the Ternary Operator?
\n\nThe answer is simple and lies in the language's philosophy: readability is more important than brevity.
\n\nRobert Griesemer (one of Go's creators) once said: "The ternary operator is a source of bugs. It is too easily misread, especially when nested". And it's true. Just recall the famous C example:
\n\nint x = (a ? b : c) ? d : e;\n\nTry to figure out what it does at a glance? The Go creators decided it's better to sacrifice one line but make the code obvious.
\n\nIn Go, the priority is clarity of intent. If you are writing a condition, let it be explicit. The ternary operator often turns into a "magic number": you look at the line and don't immediately understand the logic. This shouldn't happen in Go.
\n\nBasic Replacement: if-else as an Idiom
\n\nThe simplest and most correct way to replace the ternary is to use a regular if-else. Yes, it takes 3 lines instead of 1, but it reads effortlessly.
Example: find the maximum of two numbers.
\n\nIn C:
\nint max = (a > b) ? a : b;\n\nIn Go (idiomatic):
\nvar max int\nif a > b {\n max = a\n} else {\n max = b\n}\n\n"But this is longer!" — you might say. Yes. But look at it from another perspective: in a month, you will open the code and immediately see the logic. No parentheses, no ambiguities. Just "if a is greater than b, then max = a, else max = b".
\n\nMoreover, the Go community considers this style the only correct one for simple conditions. Don't try to "cheat" and cram everything into one line — it will be considered bad practice.
\n\nAdvanced Techniques: When You Want Brevity
\n\nSometimes you write code where if-else takes 10 lines, even though it's essentially a simple assignment. In such cases, you can apply several patterns that preserve idiomaticity but make the code more compact.
1. Helper Function
\n\nIf you often need to choose between two values, extract the logic into a function. This is especially convenient for one-liner operations.
\n\nfunc ternary(cond bool, a, b string) string {\n if cond {\n return a\n }\n return b\n}\n\n// Usage:\ngreeting := ternary(isMorning, "Good morning", "Good evening")\n\nImportant: don't name the function ternary — it's considered bad form, as you are trying to "smuggle" the ternary in through the back door. It's better to give it a meaningful name, like ifElse or choose. But if you are writing for yourself — why not?
2. Using a Map for Selection
\n\nIf the condition boils down to choosing between two options, you can use a map with boolean keys. This looks unusual, but sometimes it reads even better.
\n\nvar result = map[bo