add file copy function

This commit is contained in:
Jeffrey Paul 2020-09-21 14:53:35 -07:00
parent cdefc88d8f
commit 5d77a9a3d3
1 changed files with 26 additions and 0 deletions

26
util.go
View File

@ -2,6 +2,7 @@ package goutil
import (
"errors"
"io"
"math"
"os"
"regexp"
@ -43,3 +44,28 @@ func Mkdirp(p string) error {
}
return nil
}
// straight outta stackoverflow
// https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file-in-golang
func CopyFile(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
err = out.Sync()
return
}