Created
August 29, 2018 00:43
-
-
Save yudai/56235b4ec8ce391ec990a370fe36ce4e 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 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