generated from origadmin/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmust.go
61 lines (54 loc) · 1.41 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
60
61
package generic
// Must is a utility function that ensures a value is not nil and returns it.
// If the error is not nil, it panics with the error message.
func Must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
// Must2 is a utility function that ensures a value is not nil and returns it.
func Must2[T any, U any](v T, u U, err error) (T, U) {
if err != nil {
panic(err)
}
return v, u
}
// MustOr is a utility function that ensures a value is not nil and returns it.
// If the error is not nil, it returns the default value.
func MustOr[T any](def T, v T, err error) T {
if err != nil {
return def
}
return v
}
// MustOrZero is a utility function that ensures a value is not nil and returns it.
// If the error is not nil, it returns a zero value.
func MustOrZero[T any](v T, err error) T {
if err != nil {
return *new(T)
}
return v
}
// MustOrNil is a utility function that ensures a value is not nil and returns it.
// If the error is not nil, it returns nil.
func MustOrNil[T any](v *T, err error) *T {
if err != nil {
return nil
}
return v
}
// OrNil is a utility function that ensures a value is not nil and returns it.
func OrNil[T any](_ T, err error) error {
if err != nil {
return err
}
return nil
}
// OrNil2 is a utility function that ensures a value is not nil and returns it.
func OrNil2[T any](_, _ T, err error) error {
if err != nil {
return err
}
return nil
}