How to Walk a Directory Tree in Go (filepath.Walk vs filepath.WalkDir)

Use filepath.WalkDir for faster directory traversal by leveraging fs.DirEntry instead of the deprecated filepath.Walk.

Use filepath.WalkDir instead of filepath.Walk for better performance and modern io/fs integration. WalkDir avoids calling os.Lstat on every entry by using fs.DirEntry, which is significantly faster for large directory trees.

err := filepath.WalkDir("./path", func(path string, d fs.DirEntry, err error) error {
    if err != nil {
        return err
    }
    if d.IsDir() {
        return nil
    }
    // Process file
    return nil
})