Skip to content

Instantly share code, notes, and snippets.

@beta
Created December 26, 2017 12:43
Show Gist options
  • Save beta/87eaf1182a9024824a2011a4833c7817 to your computer and use it in GitHub Desktop.
Save beta/87eaf1182a9024824a2011a4833c7817 to your computer and use it in GitHub Desktop.
func WriteFile(...) (err error) {
...
defer func() {
if cerr := f.Close(); cerr != nil && err == nil {
err = cerr
}
}()
_, err = f.Write(data)
return err
}
func safeClose(c io.Closer, err *error) {
if cerr := f.Close(); cerr != nil && *err == nil {
*err = cerr
}
}
func WriteFile(...) (err error) {
...
defer safeClose(f, &err)
_, err = f.Write(data)
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment