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

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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?