File: die.go

package info (click to toggle)
golang-github-kisom-goutils 0.0~git20161101.0.858c9cb-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 384 kB
  • ctags: 331
  • sloc: makefile: 6
file content (29 lines) | stat: -rw-r--r-- 619 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 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)
}

// When prints the error to stderr and exits if cond is true.
func When(cond bool, fstr string, args ...interface{}) {
	if cond {
		With(fstr, args...)
	}
}