/handy

Small but useful Go generic functions.

Primary LanguageGoMIT LicenseMIT

handy

Small but useful Go generic functions.

Table of Contents

Showcase

Pointer initializer

p := handy.New(42)
fmt.Println(*p) // 42

Slice initializer

ss := handy.Slice("foo", "bar")
fmt.Println(ss) // [foo bar]

Ternary operator

c := handy.Tern(true, "foo", "bar")
fmt.Println(c) // foo

Unpack error

w := iomock.ErrOnCallWriter(1, iomock.ErrWrite)
err := handy.UnpackError(io.WriteString(w, "foobar"))
fmt.Println(err) // write error

Single-line errors.As

error type

type TestError string

func (s TestError) Error() string {
	return string(s)
}
in := fmt.Errorf("wrap: %w", TestError("error"))
fmt.Println(handy.As[TestError](in)) // error true