must

package
v0.0.0-...-d430ac9 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2024 License: Apache-2.0 Imports: 1 Imported by: 28

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[T any](val T, err error) T

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 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))

Types

This section is empty.

Jump to

Keyboard shortcuts

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