8.8 Example: Concurrent Directory Traversal
In this section, we’ll build a program that reports the disk usage of
one or more directories specified on the command line, like the Unix
du
command.
Most of its work is done by the walkDir
function below, which
enumerates the entries of the directory dir
using the
dirents
helper function.
// walkDir recursively walks the file tree rooted at dir // and sends the size of each found file on fileSizes. func walkDir(dir string, fileSizes chan<- int64) { for _, entry := range dirents(dir) { if entry.IsDir() { subdir := filepath.Join(dir, entry.Name()) walkDir(subdir, fileSizes) } else { fileSizes <- entry.Size() } } } // dirents returns the entries of directory ...
Get The Go Programming Language now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.