| by Arround The Web | No comments

Directory Walking in Go (Walkdir)

Directory traversal or directory walking refers to the process of iterating through the files and directories within a given directory and subdirectories within the file system.

Directory walking is a very useful task when you need to gather an information about a given directory. It can also allow you to create a custom directory listing utility that is tailored to your needs.

In this tutorial, we will explore all the various methods and techniques that we can use to perform the directory walking using the Go programming languages.

Golang Directory Walking

Let us learn about these methods to help us understand what directory walking is.

Method 1: Using Filepath.Walk

The filepath.Walk provides us with an efficient and easy way to traverse the directories in Go. It is available in the “filepath” package which is part of the Golang standard library which removes the reliability on external packages.

One advantage or disadvantage of this technique is that is abstracts a lot of low-level implementations of directory traversal. This can be useful when you need a quick and efficient method. However, if you need more control, you may need to dig deeper.

When using the “walk” function, you can provide a “callback” function that will be called for each visited file or directory.

The signature of the “callback” function is as follows:

func(path string, info fs.DirEntry, err error) error

 
The function supports the following parameters:

    • path – It specifies the full path to the file or directory.
    • info – It contains an information about the file or directory.
    • err – It refers to an error that may occur during traversal.

Let us look at an example on how to use this method to perform a basic directory walk:

package main
import (
    "fmt"
    "log"
    "os"
    "path/filepath"
)
func main() {
    err := filepath.Walk("/usr/local",
        func(path string, info os.FileInfo, err error) error {
            if err != nil {
                return err
            }
            fmt.Println(path, info.Size())
            return nil
        })
    if err != nil {
        log.Println(err)
    }
}

 
In this case, we perform a basic directory walking using the filepath.Walk() function and print the path of each file as well as the size. If an error occurs, during the traversal, we log it using Println from the “log” package.

Method 2: Using Ioutil.ReadDir

The second technique that we can use to traverse a directory in Go is using the “ReadDir” function from the io/ioutil package.

An example code is as follows:

package main
import (
    "fmt"
    "io/ioutil"
    "log"
    "os"
    "path/filepath"
)
func main() {
    files, err := ioutil.ReadDir("/usr/local")
    if err != nil {
        log.Fatal(err)
    }
    for _, f := range files {
        fmt.Println(f.Name())
    }
    err = filepath.Walk("/usr/local",
        func(path string, info os.FileInfo, err error) error {
            if err != nil {
                return err
            }
            fmt.Println(path, info.Size())
            return nil
        })
    if err != nil {
        log.Println(err)
    }
}

 
The code starts to list the names of the files and directories in the specified path. It then uses Walk() to recursively walk through the same directory while printing the path and size of each file.

Conclusion

In this tutorial, we introduced you to the concept of directory walking which is also known as directory traversal. We also showed you the two main methods that you can use to perform the directory walking in the Go language.

Share Button

Source: linuxhint.com

Leave a Reply