go语言扫描目录

作者: adm 分类: go 发布时间: 2022-02-11

go语言扫描目录

// 扫描当前目录下文件,不递归扫描
func scanDir(dirName string) []string {

files, err := ioutil.ReadDir(dirName)

if err != nil {
log.Println(err)
}
var fileList []string
for _, file := range files {
fileList = append(fileList, dirName+string(os.PathSeparator)+file.Name())
}
return fileList
}

// 递归扫描目录
func scanDirs(dirName string) []string {
files, err := ioutil.ReadDir(dirName)
if err != nil {
log.Println(err)
}
var fileList []string
for _, file := range files {
fileList = append(fileList, dirName + string(os.PathSeparator) + file.Name())
if file.IsDir() {
fileList = append(fileList, scanDir(dirName + string(os.PathSeparator) + file.Name())...)
}
}
return fileList
}

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!