Initial import.
This commit is contained in:
12
die/README.md
Normal file
12
die/README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
Simple fatal utilities for Go programs.
|
||||
|
||||
```
|
||||
result, err := doSomething()
|
||||
die.If(err)
|
||||
|
||||
ok := processResult(result)
|
||||
if !ok {
|
||||
die.With("failed to process result %s", result.Name)
|
||||
}
|
||||
```
|
||||
|
||||
22
die/die.go
Normal file
22
die/die.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Package die contains utilities for fatal error handling.
|
||||
package die
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// If prints the error to stderr and exits if err != nil.
|
||||
func If(err error) {
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[!] %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// With prints the message to stderr, appending a newline, and exits.
|
||||
func With(fstr string, args ...interface{}) {
|
||||
out := fmt.Sprintf("[!] %s\n", fstr)
|
||||
fmt.Fprintf(os.Stderr, out, args...)
|
||||
os.Exit(1)
|
||||
}
|
||||
Reference in New Issue
Block a user