Skip to content

Instantly share code, notes, and snippets.

@thanhminhmr
Last active January 29, 2026 01:37
Show Gist options
  • Select an option

  • Save thanhminhmr/05593d24fd3ee533e5e1970ea82e3952 to your computer and use it in GitHub Desktop.

Select an option

Save thanhminhmr/05593d24fd3ee533e5e1970ea82e3952 to your computer and use it in GitHub Desktop.

Revisions

  1. thanhminhmr revised this gist Mar 25, 2025. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions ternary.go
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,18 @@
    package ternary

    func If[T any](condition bool, a Supplier[T], b Supplier[T]) T {
    func If[T any](condition bool, ifTrue Supplier[T], ifFalse Supplier[T]) T {
    if condition {
    return a.Get()
    return ifTrue.Get()
    }
    return b.Get()
    return ifFalse.Get()
    }

    func Func[T any](supplier func() T) Supplier[T] {
    return FuncSupplier[T](supplier)
    }

    func Value[T any](value T) Supplier[T] {
    return ValueSupplier[T]{value: value}
    return ValueSupplier[T]{Value: value}
    }

    type Supplier[T any] interface {
    @@ -26,9 +26,9 @@ func (s FuncSupplier[T]) Get() T {
    }

    type ValueSupplier[T any] struct {
    value T
    Value T
    }

    func (s ValueSupplier[T]) Get() T {
    return s.value
    return s.Value
    }
  2. thanhminhmr created this gist Mar 25, 2025.
    9 changes: 9 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # go-ternary

    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.

    ## Why?

    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`.
    34 changes: 34 additions & 0 deletions ternary.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    package ternary

    func If[T any](condition bool, a Supplier[T], b Supplier[T]) T {
    if condition {
    return a.Get()
    }
    return b.Get()
    }

    func Func[T any](supplier func() T) Supplier[T] {
    return FuncSupplier[T](supplier)
    }

    func Value[T any](value T) Supplier[T] {
    return ValueSupplier[T]{value: value}
    }

    type Supplier[T any] interface {
    Get() T
    }

    type FuncSupplier[T any] func() T

    func (s FuncSupplier[T]) Get() T {
    return s()
    }

    type ValueSupplier[T any] struct {
    value T
    }

    func (s ValueSupplier[T]) Get() T {
    return s.value
    }
    28 changes: 28 additions & 0 deletions ternary_test.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    package ternary

    import "testing"

    func assertEquals[T comparable](t *testing.T, a T, b T) {
    if a != b {
    t.Errorf("assertEquals failed: %v != %v", a, b)
    }
    }

    func Test(t *testing.T) {
    assertEquals(t, If(true, Value("true"), Value("false")), "true")
    assertEquals(t, If(false, Value("true"), Value("false")), "false")

    assertEquals(t, If(true, Func(func() string {
    return "true"
    }), Func(func() string {
    t.Error("lazyEvaluate failed: this func should not be called")
    return "false"
    })), "true")

    assertEquals(t, If(false, Func(func() string {
    t.Error("lazyEvaluate failed: this func should not be called")
    return "true"
    }), Func(func() string {
    return "false"
    })), "false")
    }