Golang Program to check a directory is exist or not



In golang we have different inbuild function like Stat(), Open() and mkdir() to check whether a directory exist or not . Here we are using three functions. in the first one we are using the Stat() function present in the os package while in the second and third example we are using Open() and mkdir() function respectively to implement the result.

Method 1: Using os.stat() Method

The os package provides the Stat() function to get information about a file or directory. If the directory exists, the function returns a FileInfo structure, otherwise, it returns an error.

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

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

  • Now, start the main() function. Inside the main() initialize a new variable and assign to it the directory path as input in the form of a string.

  • Call the Stat() function present in the os package with the directory path as the argument.

  • If the function returns a FileInfo structure, the directory exists.

  • If the function returns an error, check if the error type is os.IsNotExist.

  • If the error type is os.IsNotExist, the directory does not exist.

  • If the error type is not os.IsNotExist, there is some other error, and the directory may or may not exist.

Example

In the following example, we are going to use os.stats() function to check whether a directory exists or not.

package main
import (
   "fmt"
   "os"
)

func main() {
   dir := "new"
   if _, err := os.Stat(dir); os.IsNotExist(err) {
      fmt.Println(dir, "does not exist")
   } else {
      fmt.Println("The provided directory named", dir, "exists")
   }
}

Output

new does not exist

Method 2: Using OS.OPEN() Method

The Open() function of the os package can also be used to check if a directory exists or not. This function opens a file or directory, and returns a File structure. If the directory does not exist, the function returns an error.

Syntax

func Open(name string) (file *File, err error)

The open() function is present in os package and it takes a string parameter name, which represents the path of the directory. It returns a pointer to a File structure, file, and an error, err.

Algorithm

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

  • Then start the main() function. Inside the main() take the directory path as input in the form of a string.

  • Call the os.Open() function with the directory path as the argument.

  • If the function returns a File structure, the directory exists.

  • If the function returns an error, check if the error type is os.IsNotExist.

  • If the error type is os.IsNotExist, the directory does not exist.

  • If the error type is not os.IsNotExist, there is some other error, and the directory may or may not exist.

Example

In the following example, we are going to use os.open() function to check whether a directory exists or not.

package main
import (
   "fmt"
   "os"
)

func main() {
   dir := "go"
   if _, err := os.Open(dir); os.IsNotExist(err) {
      fmt.Println("The directory named", dir, "does not exist")
   } else {
      fmt.Println("The directory namend", dir, "exists")
   }
}

Output

The directory named go does not exist

Method 3: Using mkdir() Function

In this methodwe will use the Mkdir() function of the os package to check if a given directory directory exists or not. If the directory already exists, the function returns an error otherwise it creates the directory.

Syntax

func Mkdir(name string, perm FileMode) error

The mkdir() function is present in os package and is used to create a new directory with the given name. the function takes two parameters: a string name, which represents the path of the directory, and an integer perm, which represents the permissions for the directory. It returns an error.

Algorithm

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

  • Then start the main() function. Inside the main(), take the directory path and permissions as input in the form of a string and an integer, respectively.

  • Call the os.Mkdir() function with the directory path and permissions as the arguments.

  • If the function returns an error, check if the error type is os.IsExist.

  • If the error type is os.IsExist, the directory exists.

  • If the error type is not os.IsExist, there is some other error, and the directory may or may not exist.

Example

In the following example, we are going to use mkdir () function to check whether a directory exists or not.

package main
import (
   "fmt"
   "os"
)

func main() {
   dir := "new"
   if err := os.Mkdir(dir, 0755); os.IsExist(err) {
      fmt.Println("The directory named", dir, "exists")
   } else {
      fmt.Println("The directory named", dir, "does not exist")
   }
}

Output

The directory named new does not exist

Conclusion

We have successfully compiled and executed a Golang program to check if a directory exists or not along with examples. The go language provides various methods to check if a directory exists or not. We can use the os.Stat(), os.Open(), or os.Mkdir() function to accomplish this task. It is up to the developer to choose the appropriate method based on their requirements.


Advertisements