Golang Program to check a file has readable permission or not


While working with file in go language it is very important to check if a file has readable permission or not, because without the read permissions a program cannot read the content of files. Here we will use os.Stat() function , os.Open() function, as well as ,ioutil.Readfile() function to check if a file has readable permission or not.

Algortihm

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

  • Then, store the text file into a variable and call the specified function present in the respective package.

  • If an error is generated then print it on the screen.

  • Otherwise, print that the file has readable permission by using the fmt.Println() function.

Syntax

os.Stat PATH

The stat() function is present in os package and is used to resolve the path relative to the root of the project directory. The function accepts one argument which is the length of the file along with the path where it is stored.

Example 1

The first method to check if a file has readable permission or not in Go is to use the os.Stat() function. This function returns a FileInfo type value, which describes the file. To check if the file has readable permission or not, we can use the FileInfo.Mode() method and check if the returned value includes the os.ModePerm constant.

package main

import (
   "fmt"
   "os"
)

func main() {
   file := "file.txt"
   info, err := os.Stat(file)
   if err != nil {
      fmt.Printf("error getting file info: %v", err)
      return
   }
   if info.Mode().Perm()&0444 == 0444 {
      fmt.Println("file has readable permission")
   } else {
      fmt.Println("file does not have readable permission")
   }
}

Output

file has readable permission

Example 2

One more method using which we can check if a file has readable permission or not in Go is to use the os.Open() function. The os.Open() function takes the name of the file as an argument and returns a *File type value, which represents an open file. If the file does not have readable permission, the function will return an error. To check if the file has readable permission or not, we can simply call os.Open() and check if the returned error is nil.

package main

import (
   "fmt"
   "os"
)

func main() {
   file := "file.txt"
   _, err := os.Open(file)
   if err != nil {
      fmt.Println("file does not have readable permission")
   } else {
      fmt.Println("file has readable permission")
   }
}

Output

file does not have readable permission

Example 3

The third method to check if a file has readable permission or not in Go is to use the ioutil.ReadFile() function. The ioutil.ReadFile() function takes the name of the file as an argument and returns the contents of the file as a []byte type value. If the file does not have readable permission, the function will return an error. To check if the file has readable permission or not, we can simply call ioutil.ReadFile() and check if the returned error is nil.

package main

import (
   "fmt"
   "io/ioutil"
)

func main() {
   file := "file.txt"
   _, err := ioutil.ReadFile(file)
   if err != nil {
      fmt.Println("file does not have readable permission")
   } else {
      fmt.Println("file has readable permission")
   }
}

Output

file does not have readable permission

Conclusion

We have successfully compiled and executed a go language program to check if a file has readable permission or not. We have implemented three programs here. In the first program we are using the Stat() function present in os package while in the second and third program we are using the Open() and readFile() function present in os and ioutil package respectively.

Updated on: 03-May-2023

799 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements