Ternary Operator in Go: How to Replace C and Stay Sane

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Ternary Operator — Friend or Foe?

\n\n

If you came to Go from the world of C, C++, Java, or JavaScript, you are probably used to the concise construct:

\n\n
int max = (a > b) ? a : b;
\n\n

This 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 ? :.

\n\n

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\n

In 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
\n\n

Let's go!

\n\n

Why Did Go Abandon the Ternary Operator?

\n\n

The answer is simple and lies in the language's philosophy: readability is more important than brevity.

\n\n

Robert 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\n
int x = (a ? b : c) ? d : e;
\n\n

Try 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\n

In 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\n

Basic Replacement: if-else as an Idiom

\n\n

The 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.

\n\n

Example: find the maximum of two numbers.

\n\n

In C:

\n
int max = (a > b) ? a : b;
\n\n

In Go (idiomatic):

\n
var 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\n

Moreover, 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\n

Advanced Techniques: When You Want Brevity

\n\n

Sometimes 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.

\n\n

1. Helper Function

\n\n

If you often need to choose between two values, extract the logic into a function. This is especially convenient for one-liner operations.

\n\n
func 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\n

Important: 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?

\n\n

2. Using a Map for Selection

\n\n

If 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\n
var result = map[bo                

Blogs

Book Recommendations