Skip to content

Commit

Permalink
add cond/ternary operator
Browse files Browse the repository at this point in the history
  • Loading branch information
lixiaohui committed May 5, 2022
1 parent 5e3d746 commit 3f8d89f
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cond/ternary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cond

// Ternary operator with a condition and two possible results.
// The condition is evaluated and the first result is returned if it is true,
// otherwise the second result is returned.
//
// cond.Ternary(true, "yes", "no") // "yes"
// cond.Ternary(false, "yes", "no") // "no"
func Ternary[T any](condition bool, ifTrue, ifFalse T) T {
if condition {
return ifTrue
}
return ifFalse
}

0 comments on commit 3f8d89f

Please sign in to comment.