1

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
  • 2
    Go is a very opinionated language, and explicit error propagation is one of its opinions. You can convert an error to an exit or panic, but that's only feasible in applications, not libraries. If you're not forced to use that language consider switching to a more productive language. E.g. Rust also supports Go-like explicitness but simplifies the err = fallibleOperation(); if err != nil { return err } pattern to fallibleOperation()?
    – amon
    Mar 10, 2020 at 10:08

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.