diff --git a/go.mod b/go.mod index 2267596..53d993c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module git.milar.in/milarin/adverr -go 1.15 +go 1.18 diff --git a/must.go b/must.go new file mode 100644 index 0000000..fb314b7 --- /dev/null +++ b/must.go @@ -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 +}