must

package module
v0.0.0-...-70ccf00 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2025 License: MIT Imports: 4 Imported by: 13

README

must

When Go MUST get to the point.

For those not afraid of leaving idiom for code density and DRY:

func copyFile(from, to string) (err error) {
  must.RecoverToError(&err)
  f := must.One(os.Open(from))
  g := must.One(os.Create(to))
  must.E2p(io.Copy(g, f))
  return
}

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

Constants

This section is empty.

Variables

View Source
var Ck = E2p

Functions

func E2p

func E2p(err error)

func ErrOnly2

func ErrOnly2[R any](r R, err error) error

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

func One[R any](r R, err error) R

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

func OneWrap[T any](v T, err error) func(s string) T

OneWrap wraps any errors with a string Usage: f := must.OneWrap(os.Open("file.txt"))("opening file")

func RecoverToErr

func RecoverToErr(retErr *error, w ...Wrapper)

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.

func Ternary

func Ternary[R any](cond bool, r1 R, r2 R) R

Ternary is a helper function that returns r1 if cond is true, otherwise r2.

func Three

func Three[R1 any, R2 any, R3 any](r1 R1, r2 R2, r3 R3, err error) (R1, R2, R3)

func Two

func Two[R1 any, R2 any](r1 R1, r2 R2, err error) (R1, R2)

func TwoWrap

func TwoWrap[R1 any, R2 any](r1 R1, r2 R2, err error) func(s string) (R1, R2)

TwoWrap wraps any errors with a string Usage: host, port := must.TwoWrap(net.SplitHostPort(garbageVar))(fmt.Sprint("parsing h&p: %s", garbageVar))

func With

func With[C io.Closer](c C, f func(C))

With is a helper function to close a Closer after a function is done. Usage:

must.With(must.One(os.Open("/foo")), func(f *os.File) {
  // do something with f
})

Types

type Wrapper

type Wrapper func(error) error

func Wrap

func Wrap(fmtStr string, args ...any) Wrapper

Wrap adds a message around any error. Usage: defer must.RecoverToErr(&err, must.Wrap("in Launch()"))

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL