From 5d77a9a3d362f15a6a7d787a5d331ea2d956c7e6 Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 21 Sep 2020 14:53:35 -0700 Subject: [PATCH] add file copy function --- util.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/util.go b/util.go index 5d781c7..d58bcc8 100644 --- a/util.go +++ b/util.go @@ -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 +}