Standard library / path/filepath
path/filepath.WalkFunc
Callback type for Walk; receives path, metadata when available, and any visit error. Return errors to stop or special sentinels to skip. Example uses a disposable temporary workspace. Expected stdout: '.\n'.
Formal Syntax & Structure
go syntaxgo
type WalkFunc func(path string, info fs.FileInfo, err error) errorWorking Example
Example Snippetgo
package main
import ( "fmt" "io/fs" "os" "path/filepath")
func check(e error) { if e != nil { panic(e) }}func workspace() func() { root, e := os.MkdirTemp("", "bytemind-go-") check(e) old, e := os.Getwd() check(e) check(os.Chdir(root)) return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() { defer workspace()() var fn filepath.WalkFunc = func(p string, i fs.FileInfo, e error) error { if e != nil { return e } fmt.Println(p) return filepath.SkipAll } check(filepath.Walk(".", fn))}