旅游网站作用,做衣服外贸用什么网站好,房产网 最新楼盘价格,wordpress 不同分类文件 IO 处理是程序的基础功能#xff0c;WEB 程序中通过文件 IO 实现附件的上传与下载。在 GO 中#xff0c;有多种方式可以遍历文件目录#xff0c;获取文件路径#xff0c;本文从使用层面上论述这些函数。
预先准备一个包含子目录的目录#xff0c;用于遍历测试#…文件 IO 处理是程序的基础功能WEB 程序中通过文件 IO 实现附件的上传与下载。在 GO 中有多种方式可以遍历文件目录获取文件路径本文从使用层面上论述这些函数。
预先准备一个包含子目录的目录用于遍历测试 一、filepath.Walk 函数
使用 filepath.Walk 函数可以很方便的遍历目录下的多级子文件和目录。
代码示例
func main() {rootPath : ./docs_ filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {fmt.Printf(name: %s, isDir: %t, path: %s\n, info.Name(), info.IsDir(), path)return nil})
}输出结果
name: docs, isDir: true, path: ./docs
name: admin, isDir: true, path: docs\admin
name: admin_docs.go, isDir: false, path: docs\admin\admin_docs.go
name: admin_swagger.json, isDir: false, path: docs\admin\admin_swagger.json
name: admin_swagger.yaml, isDir: false, path: docs\admin\admin_swagger.yaml
name: application.yaml, isDir: false, path: docs\application.yaml
name: content, isDir: true, path: docs\content
name: content_docs.go, isDir: false, path: docs\content\content_docs.go
name: content_swagger.json, isDir: false, path: docs\content\content_swagger.json
name: content_swagger.yaml, isDir: false, path: docs\content\content_swagger.yaml二、os.ReadDir 函数
ReadDir 函数仅遍历当前目录下的子文件和目录不会递归遍历如果需要多级遍历则需要自己实现。
该函数有两个实现分别为 os.ReadDir 和 ioutil.ReadDir 两个函数结果相同。在文件数量多的情况下 os.ReadDir 的性能会更好。新版本 Go 中 ioutil.ReadDir 已经被标记废弃了不推荐使用。
代码示例
func main() {rootPath : ./docsfiles, err : os.ReadDir(rootPath)if err ! nil {fmt.Println(err)}for _, file : range files {fmt.Printf(name: %s, isDir: %t, path: %s\n, file.Name(), file.IsDir(), rootPath/file.Name())}
}输出结果
name: admin, isDir: true, path: ./docs/admin
name: application.yaml, isDir: false, path: ./docs/application.yaml
name: content, isDir: true, path: ./docs/content三、filepath.Glob 函数
filepath.Glob 函数仅遍历当前目录下的子文件和目录不会递归遍历但可以使用通配符对遍历的文件进行过滤。
代码示例
func main() {rootPath : ./docsfilePaths, err : filepath.Glob(filepath.Join(rootPath, *))if err ! nil {fmt.Println(err)}for _, filePath : range filePaths {fmt.Println(filePath)}fmt.Println(## 查找 yaml 文件)filePaths, err filepath.Glob(filepath.Join(rootPath, *.yaml))if err ! nil {fmt.Println(err)}for _, filePath : range filePaths {fmt.Println(filePath)}
}输出结果
docs\admin
docs\application.yaml
docs\content
## 查找 yaml 文件
docs\application.yaml