Open In App

Checking the Given File Exists or Not in Golang

Last Updated : 23 Mar, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In Go language, you are allowed to check whether the given file exists or not with the help of the IsNotExist() function. If this function returns true, then it indicates that the error is known to report that the specified file or directory already does not exist and if it returns false, then it indicates that the given file or directory exists. This method also satisfied by ErrNotExist as well as some syscall errors. It is defined under the os package so, you have to import os package in your program for accessing IsNotExist() function.

Syntax:

func IsNotExist(e error) bool

Example 1:




// Golang program to illustrate how to check the
// given file exists or not in the default directory
package main
  
import (
    "log"
    "os"
)
  
var (
    myfile *os.FileInfo
    e      error
)
  
func main() {
  
    // Here Stat() function returns file info and 
    //if there is no file, then it will return an error
     
    myfile, e := os.Stat("gfg.txt")
    if e != nil {
        
      // Checking if the given file exists or not
      // Using IsNotExist() function
        if os.IsNotExist(e) {
            log.Fatal("File not Found !!")
        }
    }
    log.Println("File Exist!!")
    log.Println("Detail of file is:")
    log.Println("Name: ", myfile.Name())
    log.Println("Size: ", myfile.Size())
  
      
}


Output:

Checking the Given File Exists or Not in Golang

Example 2:




// Golang program to illustrate how to check
// the given file exists or not in given 
// directory
package main
  
import (
    "log"
    "os"
)
  
var (
    myfile *os.FileInfo
    e      error
)
  
func main() {
  
    // Here Stat() function 
    // returns file info and 
    // if there is no file, 
    // then it will return an error
     
    myfile, e := os.Stat("/Users/anki/Documents/new_folder/myfolder/hello.txt")
    if e != nil {
        
      // Checking if the given file exists or not
      // Using IsNotExist() function
        if os.IsNotExist(e) {
            log.Fatal("File not Found !!")
        }
    }
    log.Println("File Exist!!")
    log.Println("Detail of file is:")
    log.Println("Name: ", myfile.Name())
    log.Println("Size: ", myfile.Size())
  
      
}


Output:

Checking the Given File Exists or Not in Golang



Similar Reads

Checking if structure is empty or not in Golang
If the structure is empty means that there is no field present inside that particular structure. In Golang, the size of an empty structure is zero. Whenever the user wants to know if the created structure is empty or not, he can access the structure in the main function through a variable. If there doesn't exist any field inside the structure, he c
3 min read
Checking Slice of bytes for equality under Unicode case folding in Golang
In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you are allowed to compare two slices with each other wi
3 min read
Golang | Checking the string for the specified regular expression
A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc. In Go regexp, you are allowed to check whether the
3 min read
Golang | Checking the byte of slice for specified regular expression
A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc. In Go regexp, you are allowed to check whether the
3 min read
How to Create an Empty File in Golang?
Like other programming languages, Go language also allows you to create files. For creating a file it provides Create() function, this function is used to create or truncates the given named file. This method will truncate the file, if the given file is already exists. This method will create a file with mode 0666, if the given file doesn't exist.
2 min read
How to Rename and Move a File in Golang?
In the Go language, you are allowed to rename and move the existing file to a new path with the help of the Rename() method. This method is used to rename and move a file from the old path to the new path. If the given new path already exists and it is not in a directory, then this method will replace it. But OS-specific restrictions may apply when
2 min read
How to Delete or Remove a File in Golang?
In the Go language, you are allowed to remove the existing file with the help of the Remove() method. This method removes the specified file from the director or it also removes empty directories. If the given path is incorrect, then it will throw an error of type *PathError. It is defined under the os package so, you have to import os package in y
1 min read
How to Truncate a File in Golang?
In Go language, you are allowed to truncate the size of the file with the help of the Truncate() function. This function is used to truncate the size of the given file in the specified size. If the given file is a symbolic link, then it changes the size of the link's target. If this method throws an error, then it will be of type *PathError. It is
2 min read
How to Read a File Line by Line to String in Golang?
To read a file line by line the bufio package Scanner is used. Let the text file be named as sample.txt and the content inside the file is as follows: GO Language is a statically compiled programming language, It is an open-source language. It was designed at Google by Rob Pike, Ken Thompson, and Robert Grieserner. It is also known as Golang. Go la
2 min read
How to Uncompress a File in Golang?
As per Wikipedia, data compression or file compression can be understood as a process of reducing the size of a particular file/folder/whatever data while still preserving the original data. Lesser file size has a lot of benefits as it will then occupy lesser storage area giving you more space for other data to occupy, transfer the file faster as t
6 min read
Article Tags :