must

package
v0.0.0-...-3088518 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: Apache-2.0 Imports: 2 Imported by: 42

Documentation

Overview

Package must contains convenience functions for quickly exiting on fatal errors without the need for excessive "if err != nil".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Return

func Return[V any](val V, err error) V

Return is like Succeed(), except that it propagates a result value on success. This can be chained with functions returning a pair of result value and error if errors are considered fatal. For example, the following:

buf, err := os.ReadFile("config.ini")
if err != nil {
  logg.Fatal(err.Error())
}

can be shortened to:

buf := must.Return(os.ReadFile("config.ini"))

func ReturnT

func ReturnT[V any](val V, err error) func(testing.TB) V

ReturnT is a variant of Return() for use in unit tests. Instead of exiting the program, any non-nil errors are reported with t.Fatal(). For example:

buf := must.ReturnT(os.ReadFile("config.ini"))(t)

func Succeed

func Succeed(err error)

Succeed logs a fatal error and terminates the program if the given error is non-nil. For example, the following:

fileContents := []byte("loglevel = info")
err := os.WriteFile("config.ini", fileContents, 0666)
if err != nil {
  logg.Fatal(err.Error())
}

can be shortened to:

fileContents := []byte("loglevel = info")
must.Succeed(os.WriteFile("config.ini", fileContents, 0666))

func SucceedT

func SucceedT(t testing.TB, err error)

SucceedT is a variant of Succeed() for use in unit tests. Instead of exiting the program, any non-nil errors are reported with t.Fatal().

Types

This section is empty.

Jump to

Keyboard shortcuts

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