dpwtr666638 2016-07-10 16:46
浏览 420
已采纳

filepath.Walk()-我可以提供关于哪些目录不可遍历的规则吗?

I'm writing a GoLang app using Go 1.7rc1.

now I want to find all go files in a specific path. besides that I want not to walk on some directories.. for example.. hidden directories like .git.

is there a way to provide Walk() with some rules ? or.. is there a diferent libraries that provide these capabilities ?

for now this is my code:

func visit(path string, f os.FileInfo, err error) error {
    fmt.Printf("Visited: %s
", path)
    return nil
}

func main() {
    filepath.Walk(path,visit)
}

any information regarding the issue would be greatly appreciated. thanks!

  • 写回答

1条回答 默认 最新

  • dsa45132 2016-07-10 16:49
    关注

    You can skip directories by returning the error filepath.SkipDir from your visit function.

    Here's how to skip .git directories:

    func visit(path string, f os.FileInfo, err error) error {
        if f.IsDir() && f.Name() == ".git" {
            return filepath.SkipDir
        }
        fmt.Printf("Visited: %s
    ", path)
        return nil
    }
    

    The test f.IsDir() is required to avoid skipping the remainder of a directory that contains a normal file named ".git".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作