-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmust.go
66 lines (59 loc) · 1.1 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 gokit
import (
"fmt"
)
// Must 用于检查错误,如果错误不为空,则抛出异常
func Must(errors ...error) {
for _, err := range errors {
if err != nil {
panic(err)
}
}
}
// MustReturn 用于检查错误,如果错误不为空,则抛出异常
//
// Example:
//
// func Foo() (int, error) {
// // ...
// }
//
// var n int
// n = MustReturn(Foo())
func MustReturn[T any](value T, err error) T {
if err != nil {
panic(err)
}
return value
}
// MustReturn2 用于检查两个有效返回值时的错误,如果错误不为空,则抛出异常
//
// Example:
//
// func Foo() (int, string, error) {
// // ...
// }
//
// var n int
// var s string
// n, s = MustReturn2(Foo())
func MustReturn2[T1 any, T2 any](t1 T1, t2 T2, err error) (T1, T2) {
if err != nil {
panic(err)
}
return t1, t2
}
// MustTo 类型转换失败则panic
//
// Deprecated: Use MustBe() instead.
func MustTo[T any](v any) T {
return MustBe[T](v)
}
// MustBe 类型断言,失败则panic
func MustBe[T any](v any) T {
if x, ok := v.(T); ok {
return x
}
var x T
panic(fmt.Errorf("%T is not %T", v, x))
}