Back to Lo

Core Tryorx

docs/data/core-tryorx.md

1.53.0364 B
Original Source

Like Try, but returns provided fallback values in case of error; also returns a success flag. Variants cover callbacks returning 1 to 6 values.

go
value, ok := lo.TryOr(func() (int, error) {
    return 0, fmt.Errorf("boom")
}, 123)
// value == 123, ok == false

value, ok = lo.TryOr(func() (int, error) {
    return 42, nil
}, 0)
// value == 42, ok == true