Documentation
¶
Overview ¶
Must simplifies Golang into a qbasic-style (nearly) error handling. Consider the following code: import . "github.com/snadrus/must"
func Copyfile(dest, src string) (err error) {
Recover2Err(&err)
d := One(os.Open(dest))
defer d.Close()
s := One(os.Open(src))
defer s.Close()
_ = One(io.Copy(dest, erc))
return nil
}
Index ¶
- Variables
- func E2p(err error)
- func ErrOnly2[R any](r R, err error) error
- func Many(fs ...func())
- func One[R any](r R, err error) R
- func OneWrap[T any](v T, err error) func(s string) T
- func RecoverToErr(retErr *error, w ...Wrapper)
- func Ternary[R any](cond bool, r1 R, r2 R) R
- func Three[R1 any, R2 any, R3 any](r1 R1, r2 R2, r3 R3, err error) (R1, R2, R3)
- func Two[R1 any, R2 any](r1 R1, r2 R2, err error) (R1, R2)
- func TwoWrap[R1 any, R2 any](r1 R1, r2 R2, err error) func(s string) (R1, R2)
- func With[C io.Closer](c C, f func(C))
- type Wrapper
Constants ¶
This section is empty.
Variables ¶
var Ck = E2p
Functions ¶
func ErrOnly2 ¶
ErrOnly is a helper function to return an error if it is not nil. This is good for Many() when you don't need the first argument. Usage:
must.Many(
must.ErrOnly2(db.Exec("INSERT INTO foo VALUES (?)", 42))
must.ErrOnly2(db.Exec("INSERT INTO foo VALUES (?)", 43))
)
func Many ¶
func Many(fs ...func())
Many starts parallel goroutines that handle errors through panic forwarding.
func One ¶
One returns the first return of a (T, err) case, panicing the error if not nil: Usage: f := must.One(os.Open("file.txt"))
func OneWrap ¶
OneWrap wraps any errors with a string Usage: f := must.OneWrap(os.Open("file.txt"))("opening file")
func RecoverToErr ¶
RecoverToErr recovers from a panic and assigns the error to the given pointer. Usage: func foo() (err error) { defer must.RecoverToErr(&err) Wrappers like Wrap() can be used to annotate or examine errors.