dopt85756 2017-09-03 03:41
浏览 46
已采纳

filepath.Walk only file DOT files

I have a really puzzling problem with the filepath.Walk function. It only seems to find directories with that are DOT files. Such as .AndroidStudio or .arduino. It does not file any files or directories other than those if I set the root to something like /home/charles

//Watches ...Recursively walk the filesystem, entrypoint to file watching
func Watches(tops []string) {
    dirSet := make(map[string]bool)
    for _, top := range tops {
        err := filepath.Walk(top, func(path string, f os.FileInfo, err error) error {
            if err != nil {
                log.Println(err)
                return err
            }
            log.Println("File: ", path)
            if f.IsDir() {
                //Maps can only have one key that matches, duplicates will be overwritten
                dirSet[path] = true
            }
            return nil
        })
        if err != nil {
            log.Println(err)
        }
    }
}
  • 写回答

1条回答 默认 最新

  • doukuang6795 2017-09-03 04:46
    关注

    Package filepath

    import "path/filepath"
    

    func Walk

    The files are walked in lexical order,

    type WalkFunc

    If there was a problem walking to the file or directory named by path, the incoming error will describe the problem and the function can decide how to handle that error (and Walk will not descend into that directory). If an error is returned, processing stops. The sole exception is when the function returns the special value SkipDir. If the function returns SkipDir when invoked on a directory, Walk skips the directory's contents entirely. If the function returns SkipDir when invoked on a non-directory file, Walk skips the remaining files in the containing directory.

    In Walk, dot (Unicode Full Stop '.' U+002E) directory files are near first in lexical order.

    In your WalkFunc you return an error: "If an error is returned, processing stops." For example,

    if err != nil {
        log.Println(err)
        return err
    }
    

    Output:

    open /home/peter/.cache/dconf: permission denied
    

    To ignore an error return nil. For example,

    if err != nil {
        log.Println(err)
        return nil
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题