must

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 2 Imported by: 0

README

must

Turn assumed preconditions into enforced invariants. Panics when violated.

Use must when failure indicates a programmer error or misconfiguration — not for recoverable runtime conditions.

// Before: you're assuming this won't fail — but silently
_ = os.Setenv("PATH", newPath)

// After: same assumption, now enforced
err := os.Setenv("PATH", newPath)
must.BeNil(err)

When you do handle the error, must.BeNil replaces the three-line if err != nil block with one. must.Get goes further — four lines (declare, call, check, panic) become one.

What It Looks Like

// Initialization — errors here mean a bug, not a runtime condition
db := must.Get(sql.Open("postgres", cfg.DSN))
err := db.Ping()
must.BeNil(err)
// Required environment
home := must.Getenv("HOME")
// Pipeline adapter — wraps func(T)(R, error) into func(T) R
mustAtoi := must.Of(strconv.Atoi)
ints := slice.From(strings).ToInt(mustAtoi)

Making Assumptions Visible

Every _ = fn() is a hidden invariant — you're assuming the error won't happen. If it does, the program continues in a corrupt state with no indication of what went wrong.

must makes the assumption explicit. If it's wrong, you find out immediately.

Don't recover from must panics. An invariant violation means the program is in a state you didn't anticipate — recovering and continuing from that state is how silent corruption happens. If you're tempted to recover, the operation isn't an invariant — use (T, error) returns and handle the error explicitly.

Convention: For error-only returns, keep the err assignment — it reads like Go:

err := db.Ping()
must.BeNil(err)

Use must.Get when you need the value: db := must.Get(sql.Open(...)). Prefix must.Of-wrapped variables with must to signal panic behavior: mustAtoi := must.Of(strconv.Atoi).

Operations

  • Get(T, error) T — extract value or panic
  • Get2(T, T2, error) (T, T2) — two-value variant
  • BeNil(error) — panic if non-nil
  • Getenv(key) string — env var or panic
  • Of(func(T)(R, error)) func(T) R — wrap for higher-order use

See pkg.go.dev for complete API documentation, the main README for installation, and either for typed alternatives without panics.

Documentation

Overview

Package must provides functions to panic if a condition is not met.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeNil

func BeNil(err error)

BeNil panics if err is not nil.

func Get

func Get[T any](t T, err error) T

Get returns the value of a (value, error) pair of arguments unless error is non-nil. In that case, it panics.

func Get2

func Get2[T, T2 any](t T, t2 T2, err error) (T, T2)

Get2 returns the value of a (value, value, error) set of arguments unless error is non-nil. In that case, it panics.

func Getenv

func Getenv(key string) string

Getenv returns the value in the environment variable named by key. It panics if the environment variable doesn't exist or is empty.

func Of

func Of[T, R any](fn func(T) (R, error)) func(T) R

Of returns the "must" version of fn. fn must be a single-argument function.

Types

This section is empty.

Jump to

Keyboard shortcuts

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