Files
lo/docs/data/core-if.md
T
2025-10-06 17:15:49 +02:00

1.7 KiB

name, slug, sourceRef, category, subCategory, playUrl, variantHelpers, similarHelpers, position, signatures
name slug sourceRef category subCategory playUrl variantHelpers similarHelpers position signatures
If/Else if-else condition.go#L31 core condition https://go.dev/play/p/WSw3ApMxhyW
core#condition#if
core#condition#iff
core#condition#switch
core#condition#ternary
core#condition#validate
10
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) T

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)
// 1

IfF

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

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

ElseIf

Adds an ElseIf branch to an If/Else chain.

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.

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.

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.

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