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

报告相同问题?