Skip to content

Propose using filepath.WalkDir instead of filepath.Walk #1014

Closed
@neclepsio

Description

@neclepsio

Go 1.16 added filepath.WalkDir alongside filepath.Walk for performance reasons (see golang/go#42027). In my experience, traversing a big network share to retrieve few files based on their name, I got a huge speedup.

From Go 1.16 docs:

Walk is less efficient than WalkDir, introduced in Go 1.16, which avoids calling os.Lstat on every visited file or directory.

The differences between WalkDirFunc compared to filepath.WalkFunc are:

  • The second argument has type fs.DirEntry instead of fs.FileInfo.
  • The function is called before reading a directory, to allow SkipDir to bypass the directory read entirely.
  • If a directory read fails, the function is called a second time for that directory to report the error.

filepath.Walk was not deprecated, but I think filepath.WalkDir should always be preferred: even when using the information not present in fs.DirEntry, you could save time calling os.Stat only when needed.

So I propose to add a check to suggest the use of filepath.WalkDir instead of filepath.Walk.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions