Golang program to get all files present in a directory


Golang has OS package such as OS.open(), os.closs(), os.copy() and move more that are used for performing different operations with the external files. Similiary we have io package in golang that is used for performing read and write operations on the external file. Here in this article, we are going to use these two packages to print all the files in the given directory.

Method 1: Using OS Package

This program opens the provided directory and reads its contents using the os package. A slice of os.FileInfo objects, which are returned by the Readdir function and include details about each file in the directory, are returned. The file's name is printed after the Name method returns it.

Syntax

Os.Open()

This function is a part of os package. It is used to open a file to read. It takes one input i.e. the filename which will be opened.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package), os package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Assign the directory to the variable from which files are to be accessed.

  • Step 3 − In this step open the directory using os.Open function.

  • Step 4 − If any error is seen while opening the directory print the error statement and return.

  • Step 5 − Close the files which are opened using defer keyword and close function.

  • Step 6 − Then, read the directory using Readdir function and if error comes while reading the directory print the error statement and return.

  • Step 7 − Run a loop till the files read from the directory and print them on the console using fmt.Println().

Example

In this example, we will use Readdir to read the directory to print the files.

package main
import (
   "fmt"
   "os"
)

func main() {
   directory := "./" // The current directory

   files, err := os.Open(directory) //open the directory to read files in the directory
   if err != nil {
      fmt.Println("error opening directory:", err) //print error if directory is not opened
      return
   }
   defer files.Close()    //close the directory opened

   fileInfos, err := files.Readdir(-1)  //read the files from the directory
   if err != nil {
      fmt.Println("error reading directory:", err)  //if directory is not read properly print error message
      return
   }
   for _, fileInfos := range fileInfos {
      fmt.Println(fileInfos.Name()) //print the files from directory
   }
}

Output

.cache
main.go

Method 2: Using io/ioutil Package

This method uses ReadDir function from the ioutil package to read a directory's contents, and it returns a slice of os.FileInfo objects. The program then repeats over the slice and, like in the preceding example, uses the Name method to print the names of each file.

Syntax

Ioutil.ReadDir()

This function is available in the ioutil package and is used to read the director where the current working file is located. It has only one parameter and that is current directory location.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package), io/ioutil package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a directory variable and give it the directory from which files are to be accessed.

  • Step 3 − Use ReadDir function from ioutil package to read the files in the directory.

  • Step 4 − If an error comes while reading the files in directory print the error and return back.

  • Step 5 − If no error is seen, print the files of the directory using fmt.Println()

Example

In this example, we will use io/ioutil package’s ioutil.ReadDir function to obtain the files present in a directory.

package main
import (
   "fmt"
   "io/ioutil"
)

func main() {
   directory := "./" // The current directory

   files, err := ioutil.ReadDir(directory) //read the files from the directory
   if err != nil {
      fmt.Println("error reading directory:", err) //print error if directory is not read properly
      return
   }
   for _, file := range files {
      fmt.Println(file.Name()) //print the files from the directory
   }
}

Output

.cache
main.go

Conclusion

We executed the program of getting all files present in a directory using two examples. In the first example we will use os package functions and in the second example we will use io/ioutil package functions to execute the program.

Updated on: 22-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements