Last active
August 30, 2018 15:33
-
-
Save pdk/e0e7c335d56f035662b374bdc59b1e96 to your computer and use it in GitHub Desktop.
example of go 1 error handling
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 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