How to set depth for recursive iteration of directories in filepath.walk() func in Golang?

Issue

I want to search for a specific type of file inside a directory which has various sub-directories in it. I’m using filepath.walk() in Golang for this. However, I don’t want to iterate recursively beyond a max depth where I know that file can’t exist.

Is there any such pre-built function/library in Golang?

Solution

First, you should use filepath.WalkDir introduced in Go 1.16, which is more efficient than filepath.Walk.

Walk is less efficient than WalkDir, introduced in Go 1.16, which avoids calling os.Lstat on every visited file or directory.

Then, there is no way to specify the max depth as a direct argument. You have to compute the recursion depth in the WalkDirFunc.

Apparently counting separators in the filepath is an acceptable strategy (and arguably simpler than other possible tricks), so the solution might look like:

    maxDepth := 2
    rootDir := "root"
    err := filepath.WalkDir(rootDir, func(path string, d fs.DirEntry, err error) error {
        if err != nil {
            // handle possible path err, just in case...
            return err
        }
        if d.IsDir() && strings.Count(path, string(os.PathSeparator)) > maxDepth {
            fmt.Println("skip", path)
            return fs.SkipDir
        }
        // ... process entry
        return nil
    })

So with dir structure as the following:

.
└── root
    ├── a.txt
    ├── b.txt
    └── root1
        ├── a.txt
        └── root2
            ├── a.txt
            ├── b.txt
            ├── root3
            │   └── a.txt
            └── root4

and assuming root is at depth 0, the above code the above code prints:

skip root/root1/root2/root3
skip root/root1/root2/root4

Answered By – blackgreen

Answer Checked By – Robin (GoLangFix Admin)

Leave a Reply

Your email address will not be published.