On Tue, 19 Mar 2019 16:57:51 -0700 (PDT)
David Collier-Brown <dav...@spamcop.net> wrote:

> It's a known bad thing to defer a close for anything buffered, as
> discussed at
> https://www.joeshaw.org/dont-defer-close-on-writable-files/ but some
> constructs lack a Sync() call.
> 
> For example, I have code like
>         ifDB, message, err := OpenInflux(server, debug)
>         if err != nil {
>                 // If this fails, fail fast so we notice
>                 panic(fmt.Errorf("failed to open and ping influxDB,
> %s %v", message, err))
>         }
>         defer func() {
>                 err = ifDB.Close()
>                 if err != nil {
>                         // This may be a harmless, very bad thing
>                         panic(fmt.Errorf("failure closing connection
> to influxDB, %v", err))
>                 }
>         }()
> 
> 
> which closes an influxDB database, but will panic if the close fails.
> 
> Is there an idiomatic way to address this?

Any Close on file or connection should be called only if only the
call to Open function or method success.

You have two options here: either you remove the panic when Close is
error (only logging it) or call Close only if Open is success.  I
usually prefer the latter.

-- 
{ "github":"github.com/shuLhan", "site":"kilabit.info" }

-- 
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