Golang Program to check a specified file is exist or not


In golang, we have internal function like stat(), Open(0 and Readfile() that is used for checking whether a specific file exist or not. In the first example we are using the stat() method present in the os package while in the second and third example we are using Open() and ReadFile() function respectively.

Method 1: Using STAT() Method

In this method, we will use the Stat function present in os package to check if the file exists or not.

Syntax

Stat(file string)

The stat() function is present in os package and is used to get the status of a file. The function accepts the name of file as argument to the function and returns the information about a file named by a file path, including its size, mode, and modification time. If the file does not exist, the os.Stat function will return an error.

Algorithm

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

  • Then, start the main() function. Inside the main() function initialize a variable and store in it the file to be checked.

  • Now, call Stat() function present in os package and store the result in a variable.

  • Check if the error returned by os.Stat is not nil.

  • If the error is not nil, check if it is due to the file not existing using os.IsNotExist

  • If the error is due to the file not existing, print "File does not exist"

  • If the error is not due to the file not existing, print "Error occured" and the error message

  • If the error is nil, print "File exists"

Example

In this example, we are going to learn about use stat() method to check whether a specified file is exist or not

package main
import (
   "fmt"
   "os"
)

func main() {
   // enter the name of file to be checked
   file := "new.go"
   _, err := os.Stat(file)
   if err != nil {
      if os.IsNotExist(err) {
         fmt.Println("File does not exist")
      } else {
         fmt.Println("Error occured:", err)
      }
   } else {
      fmt.Println("File exists")
   }
}

Output

File does not exist

Method 2: Using open() Function

In this methodwe will write a go language program to check if a specified file exists or not by using Open() function.

Syntax

Open(file)

The Open() function is present in os package and is used to open a file for reading. The function accepts the file to be opened as argument to the function and returns a *os.File value that represents the opened file. If the file does not exist, the os.Open function will return an error.

Algorithm

  • First, we need to import the fmt and os package. Then start the main() function. Inside the main() initialize a variable of string data type and store in it the file path as a string variable that is to be checked.

  • Call os.Open function and store the result in a variable

  • Check if the error returned by os.Open is not nil

  • If the error is not nil, check if it is due to the file not existing using os.IsNotExist

  • If the error is due to the file not existing, print "File does not exist"

  • If the error is not due to the file not existing, print "Error occured" and the error message.

  • If the error is nil, print "File exists"

Example

In this example, we are going to learn about use open() method to check whether a specified file is exist or not

package main
import (
   "fmt"
   "os"
)

func main() {
   file := "test.txt"
   _, err := os.Open(file)
   if err != nil {
      if os.IsNotExist(err) {
         fmt.Println("File does not exist")
      } else {
         fmt.Println("Error occured:", err)
      }
   } else {
      fmt.Println("File exists")
   }
}

Output

File does not exist

Method 3: Using READFILE()

In this method,we will explain the go language program to check if a specified file can exists or not by using readfile. The ioutil.ReadFile function reads the entire contents of a file and returns the contents as a byte slice. If the file does not exist, the ioutil.ReadFile function will return an error.

Syntax

ioutil.ReadFile(file)

The ReadFile() function is present in ioutil package and is used to read the given file. The function accepts the file to be read as argument to the function.

Algorithm

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

  • Then, start the main() function. Inside the main() initialize a variable and store in it the extension of the file.

  • Call the ReadFile() function present in the ioutil package and store the result in the variable along with the error if generated.

  • If the error is generated, check if it is due to the nonexistence of file using os.IsNotExist() function.

  • If the error is due to the file not existing, print “File does not exist” otherwise print the error that is generated.

  • If no error is generated then, print that the file exists.

Example

In this example, we are going to learn about use READFILE () method to check whether a specified file is exist or not

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

func main() {
   file := "test.txt"
   _, err := ioutil.ReadFile(file)
   if err != nil {
      if os.IsNotExist(err) {
         fmt.Println("File does not exist")
      } else {
         fmt.Println("Error occured:", err)
      }
   } else {
      fmt.Println("File exists")
   }
}

Output

File does not exist

Conclusion

We have successfully compiled and executed a go language program to check if a specified file exists or not along with the examples. There are multiple methods to check if a specified file exists in Go, including using os.Stat, os.Open, and ioutil.ReadFile. The choice of method will depend on the specific requirements of the task at hand. Regardless of the method used, it is important of the task at hand, it is important to handle the error properly to determine if the file exists or not. The os.IsNotExist function can be used to check if the error is due to the file not existing, and any other errors can be handled accordingly.

Updated on: 22-Feb-2023

332 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements