rtx

package
v0.1.76 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2025 License: Apache-2.0 Imports: 3 Imported by: 147

README

rtx

Functions that maybe should be part of the standard go runtime.

Contains a function Must that calls log.Fatal with a nice error message if its first argument is not a nil error. Using Must aids in the pursuit of 100% code coverage, because it means that the error pathway of log.Fatal is in this package instead of inline with the code being tested.

PanicOnError is like Must, but it panics instead of logging and exiting.

It also contains ValueOrDie, which is like Must, but for functions that return a value and ValueOrPanic which panics instead of dying.

Documentation

Overview

Package rtx provides free functions that would be handy to have as part of the go standard runtime.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

func Must(err error, prefix string, args ...interface{})

Must will call log.Fatal if passed a non-nil error. The fatal message is specified as the prefix argument. If any further args are passed, then the prefix will be treated as a format string.

The main purpose of this function is to turn the common pattern of:

err := Func()
if err != nil {
    log.Fatalf("Helpful message (error: %v)", err)
}

into a simplified pattern of:

Must(Func(), "Helpful message")

This has the benefit of using fewer lines, a common error path that has test coverage, and enabling code which switches to this package to have 100% coverage.

func PanicOnError

func PanicOnError(err error, prefix string, args ...interface{})

PanicOnError will call panic if passed a non-nil error. The message to panic is the prefix argument. If further args are passed, the prefix is treated as a format string. If you don't know whether to use `Must` or `PanicOnError`, you should use `Must`.

This provides a function like Must which causes a recoverable failure, for use in things like web handlers, where the crashing and failure of a single response should not crash the server as a whole. The use of `panic()` and `recover()` in Go code is discouraged, and best practices dictate that the call to `panic()` and any corresponding `recover()` should be in the same package. PanicOnError is a simple function, so we adopt the rule that every call to PanicOnError and its corresponding `recover()` should be in the same package. For an example of this, see the code in the `scamper` package in https://github.com/m-lab/traceroute-caller

func ValueOrDie added in v0.1.76

func ValueOrDie[T any](value T, err error) T

ValueOrDie is like Must, but it returns a value instead of having no output.

It is expected this will be used like Must, but for functions that return a value, error tuple. Code like:

value, err := Func()
if err != nil {
    log.Fatalf(err)
}

can be simplified to:

value := ValueOrDie(Func())

When Must was originally added, Go didn't have generics. Now that it does, ValueOrDie is the generic version of Must. Unfortunately, because of how tuples are unpacked and passed to other functions, we can't supply a prefix or any other arguments.

func ValueOrPanic added in v0.1.76

func ValueOrPanic[T any](value T, err error) T

ValueOrPanic is like ValueOrDie, but it panics instead of exiting.

Types

This section is empty.

Jump to

Keyboard shortcuts

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