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 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址