Skip to content

Instantly share code, notes, and snippets.

@asahasrabuddhe
Last active March 22, 2019 09:14
Show Gist options
  • Save asahasrabuddhe/4411b44fde65c1575c92f04dce2369a4 to your computer and use it in GitHub Desktop.
Save asahasrabuddhe/4411b44fde65c1575c92f04dce2369a4 to your computer and use it in GitHub Desktop.
elegant way to handle errors of deferred functions
func Close(close io.Closer) {
err := close.Close()
if err != nil {
log.Println("error when trying to close resource:", err.Error())
}
}
// example use
defer res.Close() // this gives an warning about unhandled error
defer Close(res) // problem solved :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment