英文:
filepath.Walk only files DOT files
问题
我对filepath.Walk函数有一个非常困惑的问题。它似乎只能找到以点开头的目录,比如.AndroidStudio或.arduino。如果我将根目录设置为类似/home/charles的路径,它不会找到任何文件或目录。
//递归遍历文件系统,文件监视的入口点
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("文件:", path)
if f.IsDir() {
//映射只能有一个与之匹配的键,重复的将被覆盖
dirSet[path] = true
}
return nil
})
if err != nil {
log.Println(err)
}
}
}
以上是你提供的代码部分。
英文:
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
得分: 2
> Package filepath
>
> import "path/filepath"
>
> func Walk
>
> 文件按字典顺序遍历,
>
> type WalkFunc
>
> 如果在遍历到路径名为path的文件或目录时出现问题,传入的错误将描述该问题,函数可以决定如何处理该错误(Walk将不会进入该目录)。如果返回错误,处理将停止。唯一的例外是当函数返回特殊值SkipDir时。如果在目录上调用函数时返回SkipDir,Walk将完全跳过该目录的内容。如果在非目录文件上调用函数时返回SkipDir,Walk将跳过包含目录中剩余的文件。
在Walk
中,点(Unicode全角句点'.' U+002E)目录文件在字典顺序中靠前。
在你的WalkFunc
中,你返回了一个错误:"如果返回错误,处理将停止。" 例如,
if err != nil {
log.Println(err)
return err
}
输出:
open /home/peter/.cache/dconf: permission denied
要忽略一个错误,可以返回nil
。例如,
if err != nil {
log.Println(err)
return nil
}
英文:
> 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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论