Git Product home page Git Product logo

must's Introduction

"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:

func M(err error) {
  if err != nil {
    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, just create an issue, I'll quickly create a subpackage with your favourite naming convention.

must's People

Contributors

janpfeifer avatar

Stargazers

Vaughn Iverson avatar Tim Mikulski avatar xiaodao avatar Michael S. Manley avatar banshan avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.