How to terminate early in Golang Walk?
You should return an error from your walkfunc. To make sure no real error has been returned, you can just use a known error, like io.EOF
.
func Find(needle string, haystack string) (result string, err error) {
err = filepath.Walk(haystack,
filepath.WalkFunc(func(path string, fi os.FileInfo, errIn error) error {
fmt.Println(path)
if fi.Name() == needle {
fmt.Println("Found " + path)
result = path
return io.EOF
}
return nil
}))
if err == io.EOF {
err = nil
}
return
}