-
Book Overview & Buying
-
Table Of Contents
Practical Systems Programming in Go
By :
Traversing directory trees is a core operation in systems programming that involves recursively visiting directories and their contained files to perform inspection, analysis, or transformation. In this section, we focus on programmatically navigating a hierarchical file system. Go offers fs.WalkDir() and filepath.Walk(), which both provide mechanisms for recursively traversing a directory tree, but they differ in their design and flexibility. filepath.Walk() delivers each visited file or directory as an os.FileInfo, requiring extra system calls to obtain detailed information about entries, whereas fs.WalkDir() provides an fs.DirEntry, which is lighter and allows you to check properties such as whether the entry is a directory without additional overhead. Moreover, fs.WalkDir() works with any fs.FS implementation, including virtual or embedded filesystems, while filepath.Walk() operates on real filesystems only. Both functions allow the user to handle errors...