The missing ternary operator in Golang
MIT License
The missing ternary operator for Go.
It's a blazing fast, 7 lines of code package that will save you a lot of lines of code over readability.
Honestly, don't take this package too seriously. It's just a joke to keep me busy one evening.
go get github.com/julien040/go-ternary
import (
"github.com/julien040/go-ternary"
)
ternary.If(true, "foo", "bar") // "foo"
ternary.If(false, "foo", "bar") // "bar"
// Use it for pluralization
ternary.If(len(slice) > 2, "objects", "object") // "objects"
ternary.If(true, 5.4, 3.2) // 5.4
Thanks to generics, you can use any type you want.
Because of generics, you need at least Go 1.18.
Contributions are always welcome!
I accept any contribution, from code to documentation, bug reports, and feature requests.
However, I wonder if there is much to contribute to this project since it's pretty simple.