1 Star 0 Fork 0

Uni-Minds/utils

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
copy.go 1.44 KB
Copy Edit Raw Blame History
Liuxy authored 2022-10-15 14:14 . Release 1.0.0
package tools
import (
"bytes"
"encoding/gob"
"io"
"io/ioutil"
"os"
"path/filepath"
)
func DeepCopy(dst, src interface{}) error {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(src); err != nil {
return err
}
return gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)
}
// Copy copies src to dest, doesn't matter if src is a directory or a file
func Copy(src, dest string) error {
src = ExpandHomeDir(filepath.Clean(src))
dest = ExpandHomeDir(filepath.Clean(dest))
info, err := os.Stat(src)
if err != nil {
return err
}
return copy(src, dest, info)
}
// "info" must be given here, NOT nil.
func copy(src, dest string, info os.FileInfo) error {
if info.IsDir() {
return dcopy(src, dest, info)
}
return fcopy(src, dest, info)
}
func fcopy(src, dest string, info os.FileInfo) error {
f, err := os.Create(dest)
if err != nil {
return err
}
defer f.Close()
if err = os.Chmod(f.Name(), info.Mode()); err != nil {
return err
}
s, err := os.Open(src)
if err != nil {
return err
}
defer s.Close()
_, err = io.Copy(f, s)
return err
}
func dcopy(src, dest string, info os.FileInfo) error {
if err := os.MkdirAll(dest, info.Mode()); err != nil {
return err
}
infos, err := ioutil.ReadDir(src)
if err != nil {
return err
}
for _, info := range infos {
if err := copy(
filepath.Join(src, info.Name()),
filepath.Join(dest, info.Name()),
info,
); err != nil {
return err
}
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/uni-minds/utils.git
git@gitee.com:uni-minds/utils.git
uni-minds
utils
utils
v1.0.2

Search