-
Notifications
You must be signed in to change notification settings - Fork 0
/
must.go
66 lines (49 loc) · 1.33 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
62
63
64
65
66
package do
import "fmt"
// Must panic if err is not nill
func Must(err error) {
mustCheckError(err)
}
// Must1 panic if err is not nill,or return 1 result
func Must1[T any](a1 T, err error) T {
mustCheckError(err)
return a1
}
// Must2 panic if err is not nill,or return 2 result
func Must2[T1, T2 any](a1 T1, a2 T2, err error) (T1, T2) {
mustCheckError(err)
return a1, a2
}
// Must3 panic if err is not nill,or return 3 result
func Must3[T1, T2, T3 any](a1 T1, a2 T2, a3 T3, err error) (T1, T2, T3) {
mustCheckError(err)
return a1, a2, a3
}
// Must4 panic if err is not nill,or return 4 result
func Must4[T1, T2, T3, T4 any](a1 T1, a2 T2, a3 T3, a4 T4, err error) (T1, T2, T3, T4) {
mustCheckError(err)
return a1, a2, a3, a4
}
// Must5 panic if err is not nill,or return 5 result
func Must5[T1, T2, T3, T4, T5 any](a1 T1, a2 T2, a3 T3, a4 T4, a5 T5, err error) (T1, T2, T3, T4, T5) {
mustCheckError(err)
return a1, a2, a3, a4, a5
}
func mustCheckError(err error) {
if err != nil {
panic(NewError(efrom(err)))
}
}
var _ = mustCheckArgs
func mustCheckArgs(wantLength int, args ...any) {
if wantLength <= 0 {
return
}
l := len(args)
if l != wantLength {
panic(NewError(efrom(fmt.Errorf("args length is not equal %d", wantLength))))
}
if v, ok := args[l-1].(error); ok && v != nil {
panic(NewError(efrom(v)))
}
}