Back to Lo

Core If

docs/data/core-if.md

1.53.01.1 KB
Original Source

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.

go
result := lo.If(true, 1).Else(3)
// 1

IfF

Function form of If. Lazily computes the initial result when the condition is true.

go
result := lo.IfF(true, func() int {
    return 1
}).Else(3)
// 1

ElseIf

Adds an ElseIf branch to an If/Else chain.

go
result := lo.If(false, 1).ElseIf(true, 2).Else(3)
// 2

ElseIfF

Function form of ElseIf. Lazily computes the branch result when the condition is true.

go
result := lo.If(false, 1).ElseIfF(true, func() int {
    return 2
}).Else(3)
// 2

Else

Completes the If/Else chain by returning the chosen result or the default provided here.

go
result := lo.If(false, 1).ElseIf(false, 2).Else(3)
// 3

ElseF

Function form of Else. Lazily computes the default result if no previous branch matched.

go
result := lo.If(false, 1).ElseIf(false, 2).ElseF(func() int {
    return 3
})
// 3