Created
December 26, 2017 12:43
-
-
Save beta/87eaf1182a9024824a2011a4833c7817 to your computer and use it in GitHub Desktop.
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 WriteFile(...) (err error) { | |
... | |
defer func() { | |
if cerr := f.Close(); cerr != nil && err == nil { | |
err = cerr | |
} | |
}() | |
_, err = f.Write(data) | |
return err | |
} |
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 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