Files
lo/docs/data/core-assert.md
T
RelicOfTesla 8590d84fcd Support Custom Assert (#755)
* custom Assert

* disable Test Parallel

* golangcli-lint warning fix

* Document custom handler for lo.Assert

Added a section on creating a custom handler for lo.Assert.

---------

Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2026-01-08 02:23:47 +01:00

1.4 KiB

name, slug, sourceRef, category, subCategory, playUrl, variantHelpers, similarHelpers, position, signatures
name slug sourceRef category subCategory playUrl variantHelpers similarHelpers position signatures
Assert assert errors.go#L359 core error-handling https://go.dev/play/p/Xv8LLKBMNwI
core#error-handling#assert
core#error-handling#assertf
core#error-handling#validate
core#error-handling#mustx
core#error-handling#tryx
core#error-handling#tryorx
core#error-handling#trycatch
core#error-handling#trywitherrorvalue
core#error-handling#errorsas
80
func Assert(condition bool, message ...string)
func Assertf(condition bool, format string, args ...any)

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 normally

Custom handler

Replace lo.Assert and lo.Assertf with your own statement:

lo.Assertf = func(condition bool, format string, args ...any) {
		if !condition {
			  panic(fmt.Errorf("%s: %s", "customErr", fmt.Sprintf(format, args...)))
		}
}