-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lixiaohui
committed
May 5, 2022
1 parent
5e3d746
commit 3f8d89f
Showing
1 changed file
with
14 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |