workdir util package.
中文参考: Go 工作目录
import "github.com/x-mod/dir"
//初始化
workdir := dir.New(
dir.Root("/path/to/workdir"), // 默认工作目录 .
dir.Folder("config"), // 增加子文件夹 config
dir.Folder("logs"), // 增加子文件夹 logs
)
if err := workdir.Open(); err != nil {
return err
}
//常规操作
//判断子目录是否存在
//func (d *Dir) DirExists(elems ...string) (bool, error)
exist, err := workdir.DirExists("logs", "20220515") // 判断 [root]/logs/20220515
//判断文件是否存在
//func (d *Dir) Exists(elems ...string) (bool, error)
exist, err := workdir.Exists("logs", "20220515","info.log") // 判断 [root]/logs/20220515/info.log
//获取目录内的文件名
// func (d *Dir) Files(elems ...string) ([]string, error)
files, err := workdir.Files("config")
//获取目录内的子文件夹
// func (d *Dir) Folders(elems ...string) ([]string, error)
folders, err := workdir.Folders("logs")
//判断文件夹是否为空
// func (d *Dir) IsEmpty(elems ...string) (bool, error)
empty, err := workdir.IsEmpty("config")
//创建子目录
//func (d *Dir) Mkdir(elems ...string) (string, error)
pathname, err := workdir.Mkdir("logs", "20220515", "errors") // 创建 [root]/logs/20220515/errors/
//获取绝对路径
//func (d *Dir) Path(elems ...string) string
abspath := workdir.Path("logs", "20220515","info.log") // abs[root]/logs/20220515/info.log
//删除文件夹
//func (d *Dir) Remove(elems ...string) error
workdir.Remove("configs")
//删除非空文件夹
//func (d *Dir) RemoveAll(elems ...string) error
workdir.RemoveAll("logs")
in main.go
:
package main
import (
"github.com/x-mod/dir"
)
//go:embed embed/config/*
var embeddir embed.FS
func main() {
dir.EmbedFS = embeddir //全局设置内嵌
...
}
then copy embed files to the workdir directory:
import "github.com/x-mod/dir"
//初始化工作目录
workdir := dir.New(
dir.Root("/path/to/workdir"), // 默认工作目录 .
dir.Folder("config"), // 增加子文件夹 config
)
if err := workdir.Open(); err != nil {
return err
}
//将内嵌资源复制到 config 子目录
if err := workdir.EmbedSkip(1, "embed"); err != nil {
return fmt.Errorf("workdir embed: %w", err)
}