Skip to content

Commit 491fdc5

Browse files
committed
added ClassicFileCopy
1 parent 74f58ff commit 491fdc5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

samples/main-play.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,31 @@ func CopyFile(src, dst string) (err error) {
3737
return nil
3838
}
3939

40+
func ClassicCopyFile(src, dst string) error {
41+
r, err := os.Open(src)
42+
if err != nil {
43+
return fmt.Errorf("copy %s %s: %v", src, dst, err)
44+
}
45+
defer r.Close()
46+
47+
w, err := os.Create(dst)
48+
if err != nil {
49+
return fmt.Errorf("copy %s %s: %v", src, dst, err)
50+
}
51+
52+
if _, err := io.Copy(w, r); err != nil {
53+
w.Close()
54+
os.Remove(dst)
55+
return fmt.Errorf("copy %s %s: %v", src, dst, err)
56+
}
57+
58+
if err := w.Close(); err != nil {
59+
os.Remove(dst)
60+
return fmt.Errorf("copy %s %s: %v", src, dst, err)
61+
}
62+
return nil
63+
}
64+
4065
// OrgCopyFile copies the source file to the given destination. If any error occurs it
4166
// returns an error value describing the reason.
4267
func OrgCopyFile(src, dst string) (err error) {

0 commit comments

Comments
 (0)