-
Notifications
You must be signed in to change notification settings - Fork 0
/
zero_and_nil.go
44 lines (37 loc) · 1 KB
/
zero_and_nil.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
package assume
import "fmt"
// NotZero panics if the value is zero.
// Only applicable to comparable types.
//
// This function can be used to check comparable types against nil.
func NotZero[V comparable](v V) {
NotZerof(v, "zero value")
}
// NotZerof panics if the value is zero.
// Only applicable to comparable types.
func NotZerof[V comparable](v V, format string, args ...any) {
var zero V
if v == zero {
panic(fmt.Sprintf(format, args...))
}
}
// NotNilSlice panics if the slice is nil.
func NotNilSlice[V any](s []V) {
NotNilSlicef(s, "nil slice")
}
// NotNilSlicef panics if the slice is nil.
func NotNilSlicef[V any](s []V, format string, args ...any) {
if s == nil {
panic(fmt.Sprintf(format, args...))
}
}
// NotNilMap panics if the map is nil.
func NotNilMap[K comparable, V any](m map[K]V) {
NotNilMapf(m, "nil map")
}
// NotNilMapf panics if the map is nil.
func NotNilMapf[K comparable, V any](m map[K]V, format string, args ...any) {
if m == nil {
panic(fmt.Sprintf(format, args...))
}
}