Skip to content

Instantly share code, notes, and snippets.

@yudai
Created August 29, 2018 00:43
Show Gist options
  • Save yudai/56235b4ec8ce391ec990a370fe36ce4e to your computer and use it in GitHub Desktop.
Save yudai/56235b4ec8ce391ec990a370fe36ce4e to your computer and use it in GitHub Desktop.
func someCaller () error {
err := CopyFile(src, dst)
if err != nil {
return fmt.Errorf("copy file failed: %s %s: %v", src, dst, err)
}
}
func CopyFile(src, dst string) error {
r, err := os.Open(src)
if err != nil {
return fmt.Errorf("failed to open source file: %s", err)
}
defer r.Close()
w, err := os.Create(dst)
if err != nil {
return fmt.Errorf("failed to open dest file: %s", err)
}
if _, err := io.Copy(w, r); err != nil {
w.Close()
os.Remove(dst)
return fmt.Errorf("copy interrupted: %s", err)
}
if err := w.Close(); err != nil {
os.Remove(dst)
return fmt.Errorf("could not close dest: %s", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment