mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
8590d84fcd
* 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>
1.4 KiB
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 |
|
|
80 |
|
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...)))
}
}