Documentation ¶
Overview ¶
Package walk walks io/fs filesystems using an iterator style, as an alternative to the callback style of fs.WalkDir.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Walker ¶
type Walker struct {
// contains filtered or unexported fields
}
Walker provides a convenient interface for iterating over the descendants of a filesystem path. Successive calls to Next will step through each file or directory in the tree, including the root.
The files are walked in lexical order, which makes the output deterministic but requires Walker to read an entire directory into memory before proceeding to walk that directory.
Walker does not follow symbolic links found in directories, but if the root itself is a symbolic link, its target will be walked.
Example ¶
package main import ( "fmt" "os" "kr.dev/walk" ) func main() { walker := walk.New(os.DirFS("/"), "usr/lib") for walker.Next() { if err := walker.Err(); err != nil { fmt.Fprintln(os.Stderr, err) continue } fmt.Println(walker.Path()) } }
Output:
func (*Walker) Entry ¶
Entry returns the DirEntry for the most recent file or directory visited by a call to Next.
func (*Walker) Err ¶
Err returns the error, if any, for the most recent attempt by Next to visit a file or directory.
If a directory read has an error, it will be visited twice: the first visit is before the directory read is attempted and Err returns nil, giving the client a chance to call SkipDir and avoid the ReadDir entirely. The second visit is after a failed ReadDir and returns the error from ReadDir. (If ReadDir succeeds, there is no second visit.)
func (*Walker) Next ¶
Next steps to the next file or directory, which will then be available through the Path, Entry, and Err methods.
Next must be called before each visit, including the first. It returns false when the walk stops at the end of the tree.
func (*Walker) Path ¶
Path returns the path to the most recent file or directory visited by a call to Next. It contains the root of w as a prefix; that is, if New is called with root "dir", which is a directory containing the file "a", Path will return "dir/a".
func (*Walker) SkipDir ¶
func (w *Walker) SkipDir()
SkipDir causes w not to walk through the directory named by Path. No directory read will be attempted on a skipped directory. If w is not on a directory, SkipDir skips nothing.
func (*Walker) SkipParent ¶
func (w *Walker) SkipParent()
SkipParent causes w to skip the file or directory named by Path (like SkipDir) as well as any remaining items in its parent directory.