must

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: Apache-2.0 Imports: 1 Imported by: 9 modules, 22 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

"Must" Shortcuts

GoDev GitHub Go Report Card

A trivial package to provide a collection of functions shortcuts error returns to panics.

This is handy when doing small programs, or script-like programs in Go, where any failure should just panic.

It defines:

var M = func(err error) 
	if err != nil {
		log.Printf("%+v", err)
		panic(err)
	}
}

func M1[T any](value T, err error) T {
	M(err)
	return value
}

// And similarly it defines functions M2 ... to ... M9.

Example of usage:

package main

import . "github.com/janpfeifer/must"

func main() {
  fName := "my_file.txt"
  contents := M1(os.ReadFile(fName))
  M(os.Remove(fName))
}

Or, if using a Go Jupyter Notebook with GoNB:

import . "github.com/janpfeifer/must"
%%
fName := "my_file.txt"
contents := M1(os.ReadFile(fName))
M(os.Remove(fName))

Notice you can also redefine M, and all other functions will automatically follow suit:

import . "github.com/janpfeifer/must"

func init() {
	M = func(err error) {
	    if err != nil {
		    log.Fatalf("Interrupted with error: %+v", err)	
        }       	
    }
}

%%
contents := M1(os.ReadFile("my_file"))  // This will fail with log.Fatalf if err != nil.

[!NOTE] panic is like an exception, and it can be caught (with recover), if you need it at some point in the "script".

FAQ

  1. What if I don't want to panic but do something else ?
    See example of reassigning M to however you want errors to be handled.
  2. What if I don't like M as the name for these functions, and want something else ?
    If it's something generic useful to others, please just create an issue. I'll quickly create a subpackage with your favourite naming convention.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var M = func(err error) {
	if err != nil {
		log.Printf("Must not error: %+v\nPanicking ...\n\n", err)
		panic(err)
	}
}

M logs and panics if `err` is not nil.

This function is used by all other variants (M1, ..., M9), and if you want a different error behavior (like `log.Fatalf` or similar), just reassign M to your particular use, and all other functions will pick it up.

Functions

func M1

func M1[T1 any](value1 T1, err error) T1

M1 checks that there is no error with `M(err)` and then simply returns the values given.

func M2

func M2[T1 any, T2 any](value1 T1, value2 T2, err error) (T1, T2)

M2 checks that there is no error with `M(err)` and then simply returns the values given.

func M3

func M3[T1 any, T2 any, T3 any](value1 T1, value2 T2, value3 T3, err error) (T1, T2, T3)

M3 checks that there is no error with `M(err)` and then simply returns the values given.

func M4

func M4[T1 any, T2 any, T3 any, T4 any](value1 T1, value2 T2, value3 T3, value4 T4, err error) (T1, T2, T3, T4)

M4 checks that there is no error with `M(err)` and then simply returns the values given.

func M5

func M5[T1 any, T2 any, T3 any, T4 any, T5 any](value1 T1, value2 T2, value3 T3, value4 T4, value5 T5, err error) (T1, T2, T3, T4, T5)

M5 checks that there is no error with `M(err)` and then simply returns the values given.

func M6

func M6[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any](value1 T1, value2 T2, value3 T3, value4 T4, value5 T5, value6 T6, err error) (T1, T2, T3, T4, T5, T6)

M6 checks that there is no error with `M(err)` and then simply returns the values given.

func M7

func M7[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any, T7 any](value1 T1, value2 T2, value3 T3, value4 T4, value5 T5, value6 T6, value7 T7, err error) (T1, T2, T3, T4, T5, T6, T7)

M7 checks that there is no error with `M(err)` and then simply returns the values given.

func M8

func M8[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any, T7 any, T8 any](value1 T1, value2 T2, value3 T3, value4 T4, value5 T5, value6 T6, value7 T7, value8 T8, err error) (T1, T2, T3, T4, T5, T6, T7, T8)

M8 checks that there is no error with `M(err)` and then simply returns the values given.

func M9

func M9[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any, T7 any, T8 any, T9 any](value1 T1, value2 T2, value3 T3, value4 T4, value5 T5, value6 T6, value7 T7, value8 T8, value9 T9, err error) (T1, T2, T3, T4, T5, T6, T7, T8, T9)

M9 checks that there is no error with `M(err)` and then simply returns the values given.

Types

This section is empty.

Jump to

Keyboard shortcuts

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