Last active
November 13, 2018 09:59
-
-
Save petergtz/8b6cbfb5dee1a8ac1f342c560b8543cd to your computer and use it in GitHub Desktop.
In support of https://medium.com/@peter.gtz/thinking-about-new-ways-of-error-handling-in-go-2-e56d116952f1
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 CopyFile(src, dst string) fails_with error { | |
r := os.Open(src) on_error err fail fmt.Errorf("in Open %s %s: %v", src, dst, err) | |
defer r.Close() on_error {} | |
w := os.Create(dst) on_error err fail fmt.Errorf("in Create %s %s: %v", src, dst, err) | |
io.Copy(w, r) on_error err { | |
w.Close() on_error {} // explicitly ignore error | |
os.Remove(dst) on_error {} // explicitly ignore error | |
fail fmt.Errorf("in Copy %s %s: %v", src, dst, err) | |
} | |
w.Close() on_error err { | |
os.Remove(dst) on_error {} // explicitly ignore error | |
fail fmt.Errorf("in Close %s %s: %v", src, dst, err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment