must

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2025 License: MIT Imports: 0 Imported by: 0

README

Must and Panic

Tired of typing if a, err := someFunction(); err != nil?

package main

import (
	"context"

	. "github.com/nguyengg/golambda/must"
)

func main() {
	// should be dot imported for convenience.
	a := Must(someFunction())

	// variants with more return values with error being the last.
	Must0(functionThatReturnsError())
	a, b := Must2(ab())
	a, b, c := Must3(abc())

	// if you need more, you should really rethink your function.
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

func Must[H any](value H, err error) H

Must panics if the second argument is a non-nil error.

Save you from having to type out something like:

if a, err := someFunction(); err != nil {
    panic(err)
}

Which can be replaced with:

import . "github.com/nguyengg/golambda/must"
a := Must(someFunction())

func Must0

func Must0(err error)

Must0 panics if the single argument is a non-nil error.

Save you from having to type out something like:

if err := someFunction(); err != nil {
    panic(err)
}

Which can be replaced with:

import . "github.com/nguyengg/golambda/must"
Must0(someFunction())

func Must2

func Must2[A any, B any](a A, b B, err error) (A, B)

Must2 panics if the last argument is a non-nil error.

Save you from having to type out something like:

if a, b, err := someFunction(); err != nil {
    panic(err)
}

Which can be replaced with:

import . "github.com/nguyengg/golambda/must"
a, b := Must2(someFunction())

func Must3

func Must3[A any, B any, C any](a A, b B, c C, err error) (A, B, C)

Must3 panics if the last argument is a non-nil error.

Save you from having to type out something like:

if a, b, c, err := someFunction(); err != nil {
    panic(err)
}

Which can be replaced with:

import . "github.com/nguyengg/golambda/must"
a, b, c := Must3(someFunction())

Types

This section is empty.

Jump to

Keyboard shortcuts

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