Skip to content

Instantly share code, notes, and snippets.

@larryli
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larryli/9521299 to your computer and use it in GitHub Desktop.
Save larryli/9521299 to your computer and use it in GitHub Desktop.
Die err message if err not nil by Golang
// Package offers a function that die err message if err not nil.
//
// import . "gist.github.com/9521299.git"
// Die(err, something...)
//
package gist9521299
import (
"fmt"
"os"
"path/filepath"
"runtime"
)
// Die err message if err not nil.
//
func Die(err error, a ...interface{}) {
if err != nil {
_, file, line, ok := runtime.Caller(1)
if ok {
fmt.Fprintf(os.Stderr, "Die @%s #%d: ", filepath.Base(file), line)
} else {
fmt.Fprintf(os.Stderr, "Die unknown location: ")
}
a = append(a, err)
fmt.Fprintln(os.Stderr, a...)
os.Exit(1)
}
}
// Package offers a function that dump everything.
//
// import . "gist.github.com/9521299.git"
// Dump(something...)
//
package gist9521299
import (
"fmt"
"os"
"path/filepath"
"runtime"
)
// Dump everything.
//
func Dump(a ...interface{}) {
_, file, line, ok := runtime.Caller(1)
if ok {
fmt.Fprintf(os.Stdout, "Dump @%s #%d: ", filepath.Base(file), line)
} else {
fmt.Fprintf(os.Stdout, "Dump unknown location: ")
}
fmt.Fprintln(os.Stdout, a...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment