Skip to content

Instantly share code, notes, and snippets.

@rsperl
Last active July 25, 2022 16:08
Show Gist options
  • Save rsperl/eb076836c4f7e634052d755cc46e40eb to your computer and use it in GitHub Desktop.
Save rsperl/eb076836c4f7e634052d755cc46e40eb to your computer and use it in GitHub Desktop.
copy a file #go #snippet
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