must
Generic error handling with panic, recover, and defer.
For now, it must be used with gotip:
$ go install golang.org/dl/gotip@latest
$ gotip download
Usage:
// main.go
package main
import (
"fmt"
"os"
"github.com/mcesar/must"
)
func main() {
fmt.Println(f())
}
func f() (err error) {
defer must.Handle(&err)
f := must.Do(os.Open("file"))
defer f.Close()
// ...
return nil
}
To run:
$ gotip run main.go
Documentation
func Do
func Do[A any](a A, err error) A
Do returns a or panics if err != nil
func Do0
func Do0(err error)
Do panics if err != nil
func Do2
func Do2[A, B any](a A, b B, err error)
Do2 returns a and b or panics if err != nil
func Handle
func Handle(err *error)
Handle sets err to recovered value if it is an error
func Handlef
func Handlef(err *error, str string)
Handlef sets err to recovered value if it is an error, wrapped according to the formatting string specified