Last active
August 29, 2015 13:57
-
-
Save larryli/9521299 to your computer and use it in GitHub Desktop.
Die err message if err not nil by Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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