package util import ( "io" "os" "path/filepath" ) func CopyFile(src, dst string) error { source, err := os.Open(src) if err != nil { return err } defer source.Close() destDir := filepath.Dir(dst) if err := os.MkdirAll(destDir, os.ModePerm); err != nil { return err } destination, err := os.Create(dst) if err != nil { return err } defer destination.Close() _, err = io.Copy(destination, source) return err } func CreateHardLink(target, linkName string) error { // 文件夹不存在则创建 destDir := filepath.Dir(linkName) if err := os.MkdirAll(destDir, os.ModePerm); err != nil { return err } return os.Link(target, linkName) } func FileExists(filename string) bool { _, err := os.Stat(filename) return !os.IsNotExist(err) } func RemoveEmptyDir(path string) error { entries, err := os.ReadDir(path) if err != nil { return err } if len(entries) == 0 { return os.Remove(path) } return nil }