This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
Core - Conditional helpersโ
This page lists all conditional operations available in the core package of lo.
Ternaryโ
A single-line if/else that returns
ifOutput
when condition is true, otherwiseelseOutput
.result := lo.Ternary(true, "a", "b")
// result: "a"
result = lo.Ternary(false, "a", "b")
// result: "b"
// With numbers
age := 25
status := lo.Ternary(age >= 18, "adult", "minor")
// status: "adult"
// With complex expressions
x, y := 10, 20
max := lo.Ternary(x > y, x, y)
// max: 20TernaryF - Function Form
Function form of Ternary. Lazily evaluates the selected branch to avoid unnecessary work or nil dereferences.
var s *string
str := lo.TernaryF(s == nil, func() string {
return uuid.New().String()
}, func() string {
return *s
})
// str: newly generated UUID (since s is nil)
// Example with expensive operations
str = "hello"
result := lo.TernaryF(s != nil, func() string {
return *s // safe dereference only when s is not nil
}, func() string {
return "default value"
})
// result: "hello"
// Performance benefit - only executes needed branch
result = lo.TernaryF(false, func() string {
time.Sleep(1 * time.Second) // this won't execute
return "slow operation"
}, func() string {
return "fast operation" // only this executes
})
// result: "fast operation" (returns immediately)Variant:Prototypes:func Ternary[T any](condition bool, ifOutput T, elseOutput T) T
func TernaryF[T any](condition bool, ifFunc func() T, elseFunc func() T) TIf/Elseโ
A fluent conditional builder that allows chaining If/ElseIf/Else conditions.
If
Starts a fluent If/ElseIf/Else chain. Returns a builder that can be completed with
ElseIf
,Else
, etc.result := lo.If(true, 1).Else(3)
// 1IfF
Function form of If. Lazily computes the initial result when the condition is true.
result := lo.IfF(true, func() int {
return 1
}).Else(3)
// 1ElseIf
Adds an ElseIf branch to an If/Else chain.
result := lo.If(false, 1).ElseIf(true, 2).Else(3)
// 2ElseIfF
Function form of ElseIf. Lazily computes the branch result when the condition is true.
result := lo.If(false, 1).ElseIfF(true, func() int {
return 2
}).Else(3)
// 2Else
Completes the If/Else chain by returning the chosen result or the default provided here.
result := lo.If(false, 1).ElseIf(false, 2).Else(3)
// 3ElseF
Function form of Else. Lazily computes the default result if no previous branch matched.
result := lo.If(false, 1).ElseIf(false, 2).ElseF(func() int {
return 3
})
// 3Prototypes:func If[T any](condition bool, result T) *ifElse[T]
func IfF[T any](condition bool, resultF func() T) *ifElse[T]
func (i *ifElse[T]) ElseIf(condition bool, result T) *ifElse[T]
func (i *ifElse[T]) ElseIfF(condition bool, resultF func() T) *ifElse[T]
func (i *ifElse[T]) Else(result T) T
func (i *ifElse[T]) ElseF(resultF func() T) TSwitchโ
Starts a functional switch/case/default chain using a predicate value.
result := lo.Switch(2).Case(1, "1").Case(2, "2").Default("3")
// "2"Case
Adds a Case branch to a Switch chain returning a constant result.
result := lo.Switch(1).Case(1, "1").Default("?")
// "1"CaseF
Adds a Case branch that lazily computes its result.
result := lo.Switch(2).CaseF(2, func() string {
return "2"
}).Default("?")
// "2"Default
Completes the Switch chain by providing a default result when no Case matched.
result := lo.Switch(42).Default("none")
// "none"DefaultF
Function form of Default. Lazily computes the default result when no Case matched.
result := lo.Switch(0).Case(1, "1").DefaultF(func() string {
return "3"
})
// "3"Prototypes:func Switch[T comparable, R any](predicate T) *switchCase[T, R]
func (s *switchCase[T, R]) Case(val T, result R) *switchCase[T, R]
func (s *switchCase[T, R]) CaseF(val T, cb func() R) *switchCase[T, R]
func (s *switchCase[T, R]) Default(result R) R
func (s *switchCase[T, R]) DefaultF(cb func() R) R