Created
July 27, 2018 19:18
-
-
Save sgen/4add8e84836313a5bae23d55cc7aa35f to your computer and use it in GitHub Desktop.
Very basic command line application error logging in go. Use of a properly configured log.Logger or another logging package is preferable.
This file contains hidden or 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 main | |
import ( | |
"errors" | |
"fmt" | |
"os" | |
) | |
func handle(err error) { | |
if err == nil { | |
return | |
} | |
logError(err) | |
os.Exit(1) | |
} | |
func logError(err error) { | |
fmt.Fprintf(os.Stderr, "create-github-repo: %s\n", err, v...) | |
} | |
func handleF(f string, err error, v ...interface{}) { | |
if err == nil { | |
return | |
} | |
logErrorF(f, err, v...) | |
os.Exit(1) | |
} | |
func logErrorF(f string, err error, v ...interface{}) { | |
fmt.Fprintf(os.Stderr, "create-github-repo: " + f + "\n", err, v...) | |
} | |
func main() { | |
var nilError error | |
handle(nilError) | |
var err error = errors.New("an error occurred") | |
handle(err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment