goleveldb
goleveldb copied to clipboard
db.Close() example in the doc
By reading the example here: https://godoc.org/github.com/syndtr/goleveldb/leveldb Create or open a database:
// The returned DB instance is safe for concurrent use. Which mean that all
// DB's methods may be called concurrently from multiple goroutine.
db, err := leveldb.OpenFile("path/to/db", nil)
...
defer db.Close()
...
Is the example correct? We are not handling an error which could be returned by defer db.Close()
func (db *DB) Close() error
I was just checking my code with https://github.com/kisielk/errcheck , and saw it reported as not handled.
I guess correct approach is :
defer func() {
err := db.Close()
if err != nil {
//handle error
}
}()
Your concerns make sense, and the quoted approach is technically correct.
It's usual for (several, me inclusive) go programmers to ignore some errors ( It's not a recommendation! ), even with the linter complaining about that.
If I were closing the database definitely, I would: defer func(){_:=db.Close()}()
If it's important to me to know what happens there, I'ld: defer func(){log.Println(db.Close())}()
And if for any reason I want to handle the error, I'ld use your snippet, adding some real error processing.