Golang Program to Display all the directories in a directory


In this golang article, we will write a go language program to display all the directories in a given directory using the Go programming language using the open() function, using the walk function as well as using the readdir function.

Method 1

In this Example we will write a go language program to display all the directories in a directory by using the open() function present in os package. One of the simplest methods to display the directories in a given directory is to use the os.Open() function.

Example

package main

import (
   "fmt"
   "os"
)

func main() {
   dirPath := "new"
   dir, err := os.Open(dirPath)
   if err != nil {
      fmt.Println(err)
      return
   }
   defer dir.Close()

   files, err := dir.Readdir(0)
   if err != nil {
      fmt.Println(err)
      return
   }
   fmt.Println("The required directories are:")
   for _, file := range files {
      if file.IsDir() {
         fmt.Println(file.Name())
      }
   }
}

Output

The required directories are:
go directory
new directory
new1

Method 2

Another method to display the directories in a given directory is to use the filepath.Walk() function. This function takes a directory path and a callback function as input, and it walks the directory tree, calling the callback function for each file or directory in the tree. The callback function can then inspect the os.FileInfo object for each file or directory and take appropriate action, such as adding the directory name to a list.

Example

package main

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

func main() {
   dirPath := "new"
   var dirs[]string

   err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
      if info.IsDir() {
         dirs = append(dirs, info.Name())
      }
      return nil
   })
   if err != nil {
      fmt.Println(err)
      return
   }
   for _, dir := range dirs {
      fmt.Println(dir)
   }
}

Output

new
go directory
new directory
new1

Method 3

The third method to display the directories in a given directory is to use the ioutil.ReadDir() function. This function takes a directory path as input and returns a slice of os.FileInfo objects, similar to the os.Open() method. To only display the directories, we can iterate through the slice and check the IsDir() method of each os.FileInfo object.

Example

package main

import (
   "fmt"
   "io/ioutil"
)

func main() {
   dirPath := "new"
   files, err := ioutil.ReadDir(dirPath)
   if err != nil {
      fmt.Println(err)
      return
   }

   for _, file := range files {
      if file.IsDir() {
         fmt.Println(file.Name())
      }
   }
}

Output

go directory
new directory
new1

Conclusion

We have successfully compiled and executed a go language program to display all the directories in a directory. We have used three different methods to display all the directories in a given directory using Go. The choice of method will depend on the requirements of your specific use case. Regardless of the method chosen, the Go programming language provides a simple and straightforward way to accomplish this task.

Updated on: 03-May-2023

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements