Last active
July 25, 2022 16:08
-
-
Save rsperl/eb076836c4f7e634052d755cc46e40eb to your computer and use it in GitHub Desktop.
copy a file #go #snippet
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
import ( | |
"io" | |
"os" | |
) | |
func cp(dst, src string) error { | |
s, err := os.Open(src) | |
if err != nil { | |
return err | |
} | |
// no need to check errors on read only file, we already got everything | |
// we need from the filesystem, so nothing can go wrong now. | |
defer s.Close() | |
d, err := os.Create(dst) | |
if err != nil { | |
return err | |
} | |
if _, err := io.Copy(d, s); err != nil { | |
d.Close() | |
return err | |
} | |
return d.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment