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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大