-
Notifications
You must be signed in to change notification settings - Fork 0
/
must.go
59 lines (49 loc) · 1.38 KB
/
must.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package errors
import "fmt"
// MustResult panics on error. Use Recover to catch the panic.
func MustResult[T any](r T, err error) T {
mayPanicf(err, "")
return r
}
// MustResultf returns a formatter function which panics with the given formatted error message.
func MustResultf[T any](r T, err error) func(format string, params ...any) T {
return func(format string, params ...any) T {
mayPanicf(err, format, params...)
return r
}
}
// MustResult2 panics on error. Use Recover to catch the panic.
func MustResult2[A, B any](a A, b B, err error) (A, B) {
mayPanicf(err, "")
return a, b
}
// MustResult2f returns a formatter function which panics with the given formatted error message.
func MustResult2f[A, B any](a A, b B, err error) func(format string, params ...any) (A, B) {
return func(format string, params ...any) (A, B) {
mayPanicf(err, format, params...)
return a, b
}
}
// Must panics with the given error.
func Must(err error) {
mayPanicf(err, "")
}
// Mustf panics with the given error.
func Mustf(err error) func(format string, params ...any) {
return func(format string, params ...any) {
mayPanicf(err, format, params...)
}
}
func Ignore[T any](r T, _ error) T {
return r
}
func Assert(truth bool, message string) {
if !truth {
panic(message)
}
}
func Assertf(truth bool, format string, params ...any) {
if !truth {
panic(fmt.Sprintf(format, params...))
}
}