Golang Program to Search for a file in a directory


In this golang article we will write a go language program to search for a file in a directory using os.Open() function as well as using the ioutil.ReadDir() function.

Searching for a specific file in golang can be very difficult task, if you have a lot of files to search, but golang makes this a easy task with its built In functions, we will see the use case of such functions in this article.

Algorithm

  • First, we need to import the "fmt" and "os" packages.

  • Then, start the main() function. Inside the main() define the name of the directory to search in and the name of the file to search for.

  • Use os.Open to open the directory and check for any errors.

  • Use os.Readdir to read the contents of the directory and check for any errors.

  • Iterate through the slice of os.FileInfo and compare each file's name to the name of the file to search for.

  • If the name of a file in the directory matches the name of the file to search for, print a message indicating that the file has been found and return.

  • If the end of the slice of os.FileInfo is reached, print a message indicating that the file was not found.

Syntax

funcReadDir(fsys FS, name string) ([]DirEntry, error)

The ReadDir() function is present in os package and is used to read the specific files present in the directories. The function accepts the name of the directory or file to be read and returns a list of entries along with an error. If the file is successfully read then the error variable is nil.

Example 1

In this Example we will write The first method involves using the os package in Go. The os.Open function opens a directory and returns a file pointer, which can be used to read the contents of the directory. The os.Readdir function takes the file pointer and returns a slice of os.FileInfo, which contains information about each file in the directory.

package main

import (
   "fmt"
   "os"
)

func main() {
   dirname := "new"
   filename := "sample.txt"

   dir, err := os.Open(dirname)
   if err != nil {
      fmt.Println("Error opening directory:", err)
      return
   }
   defer dir.Close()

   files, err := dir.Readdir(-1)
   if err != nil {
      fmt.Println("Error reading directory:", err)
      return
   }

   for _, file := range files {
      if file.Name() == filename {
         fmt.Println("File found:", file.Name())
         return
      }
   }
   fmt.Println("File not found")
}

Output

File found: sample.txt

Example 2

In this Example we will use the ReadDir() function present in ioutil package in order to search for a file in a given directory. The ioutil package in Go provides several utility functions for reading and writing files. One of these functions, ioutil.ReadDir. this function reads the contents of a directory and returns a slice of os.FileInfo.

package main

import (
   "fmt"
   "io/ioutil"
)

func main() {
   dirname := "new"
   filename := "file.txt"

   files, err := ioutil.ReadDir(dirname)
   if err != nil {
      fmt.Println("Error reading directory:", err)
      return
   }
   for _, file := range files {
      if file.Name() == filename {
         fmt.Println("File found:", file.Name())
         return
      }
   }
   fmt.Println("File not found")
}

Output

File found: file.txt

Conclusion

We have successfully compiled and executed a go language program to search for a file in a directory along with Examples. There are several methods to search for a file in a directory in Go. Each method has its advantages and disadvantages, and the best method to use depends on the specific requirements of the task. Whether you choose to use os.Open and os.Readdir, ioutil.ReadDir, Go provides easy-to-use and efficient tools for searching for files in directories.

Updated on: 03-May-2023

815 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements