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've improved it
given an input file of
test.go2
it will generate
test.go
We might be onto something! We can just do what the javascript kids do. When the language doesn't support the thing they want, they just add it with a transpiler and call it something else!
Maybe go2 is on the nose but they did say they'll never release go2, so maybe we go add generic methods while we're at it.