Standard library / io/fs
fs.WalkDirFunc
Callback for WalkDir with path, entry and error. Returning SkipDir prunes, SkipAll stops successfully, and other errors abort traversal. Expected stdout: '.\n'.
Formal Syntax & Structure
go syntaxgo
type WalkDirFunc func(path string, d DirEntry, err error) errorWorking Example
Example Snippetgo
package main
import ( "fmt" "io/fs" "testing/fstest" "time")
func tree() fstest.MapFS { return fstest.MapFS{"a.txt": &fstest.MapFile{Data: []byte("ok"), Mode: 0644, ModTime: time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC)}, "sub/b.txt": &fstest.MapFile{Data: []byte("go"), Mode: 0644}}}func main() { var fn fs.WalkDirFunc = func(p string, d fs.DirEntry, e error) error { if e != nil { return e } fmt.Println(p) return fs.SkipAll } fs.WalkDir(tree(), ".", fn)}