Yes, I know—yet another attempt at bringing a ternary-like experience to Go. But hey, Go doesn’t have one, and I wasn’t around when the last million were written.
Because Go doesn't have a ternary operator, and according to the official FAQ, it likely never will. The reasoning? To prevent developers from writing "impenetrably complex expressions." But let's be real—poor coding practices exist in all forms. Instead of outright banning a useful construct, wouldn’t compiler warnings for overly complicated ternary expressions have been a more reasonable approach?
Since that's not happening, here’s go-ternary
—because sometimes, a one-liner is just nicer than an if-else
.
I remember probably 7-8 years ago the JS guru Eric Elliot had an article titled "Nested Ternaries are great".
Then I saw one in the wild in a React component not too long after.
It was daisy chained from here to infinity and beyond into the abyss.
I could but only catch a glimpse of the very last
:
as it fell into the event horizon.Such beauty.
Such sadness.
In late 2021, it was raining - cold outside - I was bored. Opened my browser and started surfing.
Found an article about clean code and the author, another respected JS guru, was begging people:
"Please stop nesting ternaries."
I shall nest this ternary however. It begs me thus to travel into yon abyss.