Last active
March 22, 2019 09:14
-
-
Save asahasrabuddhe/4411b44fde65c1575c92f04dce2369a4 to your computer and use it in GitHub Desktop.
elegant way to handle errors of deferred functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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