Skip to content

Instantly share code, notes, and snippets.

@pdk
Last active August 30, 2018 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdk/e0e7c335d56f035662b374bdc59b1e96 to your computer and use it in GitHub Desktop.
Save pdk/e0e7c335d56f035662b374bdc59b1e96 to your computer and use it in GitHub Desktop.
example of go 1 error handling
func CopyFile(src, dst string) error {
r, err := os.Open(src)
if err != nil {
return fmt.Errorf("copy %s %s: %v", src, dst, err)
}
defer r.Close()
w, err = os.Create(dst)
if err != nil {
return fmt.Errorf("copy %s %s: %v", src, dst, err)
}
defer w.Close()
_, err = io.Copy(w, r)
if err != nil {
return fmt.Errorf("copy %s %s: %v", src, dst, err)
}
err = w.Close()
if err != nil {
return fmt.Errorf("copy %s %s: %v", src, dst, err)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment