must added

This commit is contained in:
Timon Ringwald 2022-09-15 17:53:18 +02:00
parent 2425bfdc5f
commit 9b95bdb3dd
2 changed files with 38 additions and 1 deletions

2
go.mod
View File

@ -1,3 +1,3 @@
module git.milar.in/milarin/adverr module git.milar.in/milarin/adverr
go 1.15 go 1.18

37
must.go Normal file
View File

@ -0,0 +1,37 @@
package adverr
// Must panics if err is not nil.
// It returns value otherwise
func Must[T any](value T, err error) T {
if err != nil {
panic(err)
}
return value
}
// Must2 panics if err is not nil.
// It returns a and b otherwise
func Must2[A, B any](a A, b B, err error) (A, B) {
if err != nil {
panic(err)
}
return a, b
}
// Must3 panics if err is not nil.
// It returns a, b and c otherwise
func Must3[A, B, C any](a A, b B, c C, err error) (A, B, C) {
if err != nil {
panic(err)
}
return a, b, c
}
// Must4 panics if err is not nil.
// It returns a, b and c otherwise
func Must4[A, B, C, D any](a A, b B, c C, d D, err error) (A, B, C, D) {
if err != nil {
panic(err)
}
return a, b, c, d
}