Conditional - Core helpers
This page lists all conditional operations available in the core package of lo.
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!
Ternaryโ
A single-line if/else that returns
ifOutputwhen condition is true, otherwiseelseOutput.result := lo.Ternary(true, "a", "b")// result: "a"result = lo.Ternary(false, "a", "b")// result: "b"// With numbersage := 25status := lo.Ternary(age >= 18, "adult", "minor")// status: "adult"// With complex expressionsx, y := 10, 20max := 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 *stringstr := 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 operationsstr = "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 branchresult = lo.TernaryF(false, func() string {time.Sleep(1 * time.Second) // this won't executereturn "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) Tfunc 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) Tfunc (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, callback func() R) *switchCase[T, R]func (s *switchCase[T, R]) Default(result R) Rfunc (s *switchCase[T, R]) DefaultF(callback func() R) R