Skip to content

Instantly share code, notes, and snippets.

@pdk
Created August 30, 2018 15:06
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/1fdb9f9158157af2fa341449a122c46b to your computer and use it in GitHub Desktop.
Save pdk/1fdb9f9158157af2fa341449a122c46b to your computer and use it in GitHub Desktop.
naive refactor of go 1 error handling
func CopyFile(src, dst string) error {
checkErr := func(err error) {
if err != nil {
return fmt.Errorf("copy %s %s: %v", src, dst, err)
}
}
r, err := os.Open(src)
checkErr(err)
defer r.Close()
w, err = os.Create(dst)
checkErr(err)
defer w.Close()
_, err = io.Copy(w, r)
checkErr(err)
err = w.Close()
checkErr(err)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment