gpt4 book ai didi

go - Walk文件夹时如何过滤子文件夹

转载 作者:IT王子 更新时间:2023-10-29 01:55:46 26 4
gpt4 key购买 nike

现在我正在使用 Walk 浏览我的文件夹,我想在扫描时过滤一些文件夹。

err := filepath.Walk("/home", func(path string, f os.FileInfo, err error) error {
...
})

文件夹结构是:

  home  
/ | \
a b c

我可以做一些异常(exception)列表 filepath.Walk 扫描文件夹 a 吗?也就是说,我不想将文件夹 a 中的任何文件添加到我的扫描结果中。

最佳答案

来自 WalkFuc 的文档:

If an error is returned, processing stops. The sole exception is that if path is a directory and the function returns the special value SkipDir, the contents of the directory are skipped and processing continues as usual on the next file.

因此,只需将传递给 filepath.Walk 的函数设为返回 filepath.SkipDir什么时候path是您要跳过的目录。

关于go - Walk文件夹时如何过滤子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23379066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com