I am new to Golang and I've seen it is very common to check for errors all the time. I am trying to find a way to not have my code polluted with "if error { log... }" or "if error { exit }". What do you think of a function like:
fun exitIfError(error err) {
if error != nil {
exit(1)
}
}
That body of the function would be otherwise spread throughout the main function several times, but it would probably make less explicit where the program exits, any opinions?
1 Answer 1
Go Proverbs - Rob Pike - Gopherfest - November 18, 2015
Don't just check errors, handle them gracefully.
A Go package may be used in a client program that supports tens of thousands of users concurrently. Just because you have a problem in one goroutine, don't crash everybody.
err = fallibleOperation(); if err != nil { return err }
pattern tofallibleOperation()?