walk

package module
Version: v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2021 License: MIT Imports: 3 Imported by: 0

README

Package walk walks io/fs filesystems using an iterator style,
as an alternative to the callback style of fs.WalkDir.

https://kr.dev/walk

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 New

func New(fsys fs.FS, root string) *Walker

New returns a new Walker rooted at root on filesystem fsys.

func (*Walker) Entry

func (w *Walker) Entry() fs.DirEntry

Entry returns the DirEntry for the most recent file or directory visited by a call to Next.

func (*Walker) Err

func (w *Walker) Err() error

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

func (w *Walker) Next() bool

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

func (w *Walker) Path() string

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.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto