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 - Error handling helpersโ
This page lists all error handling operations available in the core package of lo.
Validateโ
Creates an error when a condition is not met; returns nil when it is.
slice := []string{"a"}
err := lo.Validate(len(slice) == 0, "Slice should be empty")
// error("Slice should be empty")
err := lo.Validate(len(slice) == 0, "Slice should be empty but contains %v", slice)
// error("Slice should be empty but contains [a]")Prototype:func Validate(ok bool, format string, args ...any) error
MustXโ
Panics if err is an error or false, returning successful values otherwise. Variants cover functions returning 0 to 6 values.
// returns 10, panics if err is not nil
v := lo.Must(strconv.Atoi("10"))
// panics with custom message
lo.Must0(fmt.Errorf("boom"), "failed to parse")
// panics if myFunc returns an error
func myFunc() (int, string, float64, bool, error) { ... }
a, b, c, d := lo.Must4(myFunc())Variant:Prototypes:func Must[T any](val T, err any, messageArgs ...any) T
func Must0(err any, messageArgs ...any)
func Must1[T any](val T, err any, messageArgs ...any) T
func Must2[T1, T2 any](val1 T1, val2 T2, err any, messageArgs ...any) (T1, T2)
func Must3[T1, T2, T3 any](val1 T1, val2 T2, val3 T3, err any, messageArgs ...any) (T1, T2, T3)
func Must4[T1, T2, T3, T4 any](val1 T1, val2 T2, val3 T3, val4 T4, err any, messageArgs ...any) (T1, T2, T3, T4)
func Must5[T1, T2, T3, T4, T5 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, err any, messageArgs ...any) (T1, T2, T3, T4, T5)
func Must6[T1, T2, T3, T4, T5, T6 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, val6 T6, err any, messageArgs ...any) (T1, T2, T3, T4, T5, T6)TryXโ
Calls a function and returns false in case of error or panic. Variants cover callbacks returning 0 to 6 values.
ok := lo.Try(func() error {
// return an error to mark failure
return fmt.Errorf("boom")
})
// ok == false
ok = lo.Try0(func() {
// panics are caught and return false
panic("boom")
})
// ok == false
ok = lo.Try2(func() (int, error) {
return 42, nil
})
// ok == trueVariant:Prototypes:func Try(callback func() error) bool
func Try0(callback func()) bool
func Try1(callback func() error) bool
func Try2[T any](callback func() (T, error)) bool
func Try3[T, R any](callback func() (T, R, error)) bool
func Try4[T, R, S any](callback func() (T, R, S, error)) bool
func Try5[T, R, S, Q any](callback func() (T, R, S, Q, error)) bool
func Try6[T, R, S, Q, U any](callback func() (T, R, S, Q, U, error)) boolTryOrXโ
Like Try, but returns provided fallback values in case of error; also returns a success flag. Variants cover callbacks returning 1 to 6 values.
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 == trueVariant:Prototypes:func TryOr[A any](callback func() (A, error), fallbackA A) (A, bool)
func TryOr1[A any](callback func() (A, error), fallbackA A) (A, bool)
func TryOr2[A, B any](callback func() (A, B, error), fallbackA A, fallbackB B) (A, B, bool)
func TryOr3[A, B, C any](callback func() (A, B, C, error), fallbackA A, fallbackB B, fallbackC C) (A, B, C, bool)
func TryOr4[A, B, C, D any](callback func() (A, B, C, D, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D) (A, B, C, D, bool)
func TryOr5[A, B, C, D, E any](callback func() (A, B, C, D, E, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D, fallbackE E) (A, B, C, D, E, bool)
func TryOr6[A, B, C, D, E, F any](callback func() (A, B, C, D, E, F, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D, fallbackE E, fallbackF F) (A, B, C, D, E, F, bool)TryWithErrorValueโ
Runs a function and returns the error value (panic or error) along with a boolean indicating success.
err, ok := lo.TryWithErrorValue(func() error { panic("error") })
// err == "error", ok == falsePrototype:func TryWithErrorValue(callback func() error) (errorValue any, ok bool)
TryCatchโ
Calls the catch function when the callback errors or panics.
caught := false
lo.TryCatch(func() error { panic("error") }, func() { caught = true })
// caught == truePrototype:func TryCatch(callback func() error, catch func())
TryCatchWithErrorValueโ
Calls the catch function with the error value when the callback errors or panics.
lo.TryCatchWithErrorValue(
func() error {
panic("error")
},
func(val any) {
fmt.Println(val)
},
)Prototype:func TryCatchWithErrorValue(callback func() error, catch func(any))
ErrorsAsโ
A generic wrapper around
errors.As
that returns the typed error and a boolean indicating success.if rateLimitErr, ok := lo.ErrorsAs[*RateLimitError](err); ok {
// handle
}Variant:Prototype:func ErrorsAs[T error](err error) (T, bool)
Assertโ
Does nothing when condition is true; otherwise panics.
// Base variant with optional message
age := 12
lo.Assert(age >= 15, "user age must be >= 15")
// panics: "user age must be >= 15"
// Without message - panics with default message
x := -1
lo.Assert(x > 0)
// panics: "assertion failed: condition is not true"
// Formatted variant with custom message
age = 12
lo.Assertf(age >= 15, "user age must be >= 15, got %d", age)
// panics: "user age must be >= 15, got 12"
// When condition is true - no panic
age = 20
lo.Assert(age >= 15, "user age must be >= 15")
// continues normallyVariant:Prototypes:func Assert(condition bool, message ...string)
func Assertf(condition bool, format string, args ...any)