On 16/10/16 23:48, antoniosun...@gmail.com wrote:
[snip]

The reason that I didn't do it, is because I don't know how to do it. In
essence, that "f.Close()" that you worried about is wrapped in "defer
f.Close()" in SaveState, which in turn wrapped in "defer SaveState" in a
function. I.e., all is happening during the program tear-down phase. If
we are not planting a "log.Fatal(err)" bomb in SaveState itself, how do
you think we should check it properly?


If you are doing a lot of error handling just before returning from a function, take a look at the following:

https://github.com/ksugxx/swissarmy/blob/master/knives/multierr.go

The package itself is a bit of a "be-all end-all" for a few of my projects, so it lacks separation-of-concern and it may not make sense to import the entire pkg. Copy-pasting just the file linked above should do it for the error handling. It's just 80~ sloc.

Short summary:

func myfunc(...) (err error) {
  defer func() {
    err = MultiErr(err, db.Close(), resp.Body.Close(), f.Close())
  }()
  // do a lot of things
  return
}
// (didn't check for syntax error)

If every error is nil, then returned err will also be nil. Otherwise, it will be something like (e.g when 2 out of 4 errors are not nil):
err.Error() == "[division by zero] [database timed out]"

If 1 error:
err.Error() == "database timed out"

If N out of 4 error is not nil, N > 0, expect some performance hit from reflection. If N == 0 (which should be most of the time), the code will not touch reflection.


I consulted this mailing list a while back when writing this, so maybe this is also a good opportunity to get some critique.



Thanks

--
You received this message because you are subscribed to the Google
Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to golang-nuts+unsubscr...@googlegroups.com
<mailto:golang-nuts+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to