小工具知识网

当前位置: 首页 > 行业知识 > GO 的 Web 开发系列(六)—— 遍历路径下的文件

行业知识

GO 的 Web 开发系列(六)—— 遍历路径下的文件

2026-10-21 23:27:52 来源:互联网转载

���件 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: ./docsname: admin, isDir: true, path: docs\adminname: admin_docs.go, isDir: false, path: docs\admin\admin_docs.goname: 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.yamlname: content, isDir: true, path: docs\contentname: content_docs.go, isDir: false, path: docs\content\content_docs.goname: content_swagger.json, isDir: false, path: docs\content\content_swagger.jsonname: 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 := "./docs"files, 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/adminname: application.yaml, isDir: false, path: ./docs/application.yamlname: content, isDir: true, path: ./docs/content

三、filepath.Glob 函数

filepath.Glob 函数仅遍历当前目录下的子文件和目录,不会递归遍历,但可以使用通配符对遍历的文件进行过滤。

代码示例:

func main() {rootPath := "./docs"filePaths, 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\admindocs\application.yamldocs\content## 查找 yaml 文件    docs\application.yaml

上一篇:SunshineAngle怎么读?

下一篇:磁链下载软件全解析,轻松掌握高效下载技巧!