W3Basic Logo

Go List All Files in a Directory

In Go (also known as Golang), listing all files in a directory is a common task that can be accomplished using several built-in packages such as os, ioutil, and filepath.

Each package provides different functions and methods to list files in a directory. For example, the os package provides the ReadDir function, the ioutil package provides the ReadDir function, and the filepath package provides the Walk and Glob functions.

In this article, we will look into different ways to list all files in a directory in Golang with examples.


Using the os package

The os.ReadDir function lists all files in a directory and returns a slice of os.FileInfo objects. These objects contain information about the files in the directory, such as the file name, size, and permissions.

package main

import (
    "fmt"
    "os"
)

func main() {
    // Define the directory path
    dir := "C:\\Personal\\Test"

    // Use the os.ReadDir function to get a list of files in the directory
    files, err := os.ReadDir(dir)
    if err != nil {
        fmt.Println(err)
    }

    // Iterate through the list of files
    for _, file := range files {
        // Print the name of each file
        fmt.Println(file.Name())
    }
}

Output

go.txt
markdown.txt
python.txt
sql.txt

Using the ioutil package

The ioutil.ReadDir function lists all files in a directory and returns a slice of ioutil.FileInfo objects. These objects contain information about the files in the directory, such as the file name, size, and permissions.

package main

import (
    "fmt"
    "io/ioutil"
)

func main() {
    // Define the directory path
    dir := "C:\\Personal\\Test"

    // Use the ioutil.ReadDir function to get a list of files in the directory
    files, err := ioutil.ReadDir(dir)
    if err != nil {
        fmt.Println(err)
    }

    // Iterate through the list of files
    for _, file := range files {
        // Print the name of each file
        fmt.Println(file.Name())
    }
}

Output

go.txt
markdown.txt
python.txt
sql.txt

Using the filepath.Walk function

The filepath.Walk function takes two arguments: the directory path and a callback function. The callback function is called for each file and directory in the directory tree. It takes three arguments: the file path, an os.FileInfo object containing information about the file, and an error.

In this example, we're using an anonymous function as the callback. Inside the function, we're checking if the current file is a directory by calling the IsDir() method of the os.FileInfo struct. If it is a directory, we return nil to skip it. If it's a file, we print the path of the file.

package main

import (
    "fmt"
    "os"
    "path/filepath"
)

func main() {
    // Define the directory path
    dir := "C:\\Personal\\Test"

    // Use the filepath.Walk function to list all files in the directory recursively
    filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
        // Check if the current file is a directory
        if info.IsDir() {
            return nil
        }
        // Print the name of each file
        fmt.Println(path)
        return nil
    })
}

Output

C:\Personal\Test\go.txt
C:\Personal\Test\markdown.txt
C:\Personal\Test\python.txt
C:\Personal\Test\sql.txt

Using the filepath.Glob function

The filepath.Glob function lists all files in a directory that matches a pattern. It takes a pattern as an argument and returns a slice of strings, where each string is the path of a file that matches the pattern.

package main

import (
    "fmt"
    "path/filepath"
)

func main() {
    // Define the directory path
    dir := "C:\\Personal\\Test"
    // Use the filepath.Glob function to get a list of files that match a pattern
    files, _ := filepath.Glob(dir + "/*")
    for _, file := range files {
        // Print the name of each file
        fmt.Println(file)
    }
}

Output

C:\Personal\Test\go.txt
C:\Personal\Test\markdown.txt
C:\Personal\Test\python.txt
C:\Personal\Test\sql.txt

Conclusion

In conclusion, listing all files in a directory in Go is a simple task that can be performed using several built-in packages such as os, ioutil, and filepath. Each package provides different functions and methods to list files in a directory.

© 2023 W3Basic. All rights reserved.

Follow Us: