Golang Program to check if a file is directory or a file


In Go, a directory is a special type of file that holds other files and directories. A directory is used to organize files on a file system and provides a structure to the file system. Go provides several functions in the os package to work with directories, including: os.mkdir, os.remove, os.open, etc.

Syntax

func Stat(name string) (fi FileInfo, err error)

The Stat() function is present in os package and is used to check the status of a particular directory. The function takes a string parameter name, which represents the path of the directory. It returns a FileInfo structure, fi, and an error, err.

Algorithm

  • Step 1 − First, we need to import the fmt and os packages.

  • Step 2 − Then start the main() function. Inside the main initialize a variable named file and store in it the extension where the file or directory is present.

  • Step 3 − Now, check that whether the path provided is of a file or directory by using IsDir() method. If the path is of a directory then print that it is a directory.

  • Step 4 − Otherwise, print that the path is of a file.

Example 1

In this example, we will use the isDir() function present in os package. the program will display the result as a directory or file depending on whether the path provided by the user leads to the file or not.

package main
import (
   "fmt"
   "os"
)

func main() {
   // providing the path of the file
   file := "C:/Users/LENOVO/Desktop/go" 

   info, err := os.Stat(file)
   if err != nil {
      fmt.Println(err)
      return
   }

   if info.IsDir() {
      fmt.Println("It is a directory")
   } else {
      fmt.Println("It is a file")
   }
}

Output

It is a directory

Example 2

In this example, we will use Lstat() method present in os package in order to find that whether the file provided by the user is a file or a directory.

package main
import (
   "fmt"
   "os"
)

func main() {
   // giving the extension of the file
   file := "C:/Users/LENOVO/Desktop/go/newFile.txt" 
   
   fileInfo, err := os.Lstat(file)
   if err != nil {
      fmt.Println(err)
      return
   }

   mode := fileInfo.Mode()
   if mode.IsDir() {
      fmt.Println("It is a directory")
   } else if mode.IsRegular() {
      fmt.Println("It is a regular file")
   } else {
      fmt.Println("It is something else")
   }
}

Output

It is a regular file

Example 3

In this example, we will use STAT() method present in OS package in order to find that whether the file provided by the user is a file or a directory.

package main
import (
   "fmt"
   "os"
)

func main() {
   // ENTER THE EXTENSION OF THE FILE OR DIRECTORY
   file := "C:/Users/LENOVO/Desktop/go/new.go" 
   fileInfo, err := os.Stat(file)
   if err != nil {
      fmt.Println(err)
      return
   }
   if fileInfo.Mode().IsDir() {
      fmt.Println("It is a directory")
   } else {
      fmt.Println("It is a file")
   }
}

Output

It is a file

Example 4

In this example, we will use isRegular() method in order to find that whether the file provided by the user is a file or a directory.

package main
import (
   "fmt"
   "os"
)

func main() {
   // Replace this with the path to your file
   file := "C:/Users/LENOVO/Desktop/go" 
   fileInfo, err := os.Stat(file)
   if err != nil {
      fmt.Println(err)
      return
   }
   if fileInfo.Mode().IsRegular() {
      fmt.Println("It is a file")
   } else {
      fmt.Println("It is a directory")
   }
}

Output

It is a directory

Conclusion

We have successfully compiled and executed a go language program to check if the given file is a file or directory along with examples. here we have used four different approaches to check the result. In the first and second program we have used isDir() and lstat() function while in the third and fourth program we are using stat() and IsRegular() function respectively.

Updated on: 22-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements