must

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package must provides helper functions that wrap calls returning an error and panic if the error is non-nil. This is intended to reduce boilerplate in specific, controlled contexts.

WARNING: The functions in this package should be used with extreme care. They intentionally convert a recoverable error into a non-recoverable panic. This is only appropriate in specific situations where an error is considered a bug in the program, not a predictable runtime failure.

Appropriate Use Cases:

  • Program initialization (e.g., in `init` functions or `main`): parsing hardcoded configuration, compiling essential regular expressions, or setting up database connections that are required for the application to start.

  • Test setup: When setting up test fixtures where a failure indicates a broken test, not a feature to be tested.

Example:

// Instead of:
// re, err := regexp.Compile(`\w+`)
// if err != nil {
// 	panic(err)
// }

// Use must.Must for cleaner initialization code:
re := must.Must(regexp.Compile(`\w+`))

DO NOT use these functions for regular application logic where errors are expected (e.g., handling user input, network requests, file I/O).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cast

func Cast[T any](v any) T

Cast performs a type assertion and panics if it fails. It provides a more informative panic message than a raw type assertion.

func Do

func Do[T any](v T, err error) T

Do panics if err is not nil, otherwise it returns the value v. It is useful for wrapping function calls that return a value and an error, where the error is not expected.

func Do2

func Do2[T any, U any](v1 T, v2 U, err error) (T, U)

Do2 is similar to Do, but for functions that return two values and an error.

Types

This section is empty.

Jump to

Keyboard shortcuts

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