Skip to content

Instantly share code, notes, and snippets.

@petergtz
Last active November 13, 2018 09:59
Show Gist options
  • Save petergtz/8b6cbfb5dee1a8ac1f342c560b8543cd to your computer and use it in GitHub Desktop.
Save petergtz/8b6cbfb5dee1a8ac1f342c560b8543cd to your computer and use it in GitHub Desktop.
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