Golang Program to check a file has writable permission or not


In Golang, we can use openfile() and Stat() function to check whehter a file has writable permission or not. Here we have written two examples. in the first one we are using OpenFile() method present in the os package while in the second one we are using the Stat() method present in the os package respectively.

Method 1: Using OpenFile() Method

In this method, we will use the OpenFile() function to check the write permission of a file. This function opens a file with the given name and options, and returns a pointer to a File structure.

Syntax

func OpenFile(name string, flag int, perm FileMode) (*File, error)

The OpenFile() function is present in os package and is used to

The os.OpenFile() function takes three arguments −

  • name − The name of the file.

  • flag − The mode in which the file should be opened, for example, os.O_RDONLY for read-only mode, os.O_RDWR for read-write mode, etc.

  • perm − The file permissions.

Algorithm

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

  • Then, start the main() function. Inside the main() store the file whose writable permission is to be checked in a variable.

  • Define the name of the file to be checked for write permission.

  • Call the os.OpenFile() function, passing the name of the file, the os.O_RDWR or os.O_WRONLY flag, and the file permissions (0666 by default).

  • Check for errors returned by the os.OpenFile() function. If an error is returned, the file is not writable.

  • If no errors are returned, the file is writable.

Example

In this example, we are going to use OpenFile() method to check a file has writable permission or not.

package main
import (
   "fmt"
   "os"
)

func main() {
   file := "newFile.txt"
   f, err := os.OpenFile(file, os.O_RDWR, 0666)
   if err != nil {
      fmt.Printf("%s is not writable.\n", file)
   } else {
      fmt.Printf("%s is writable.\n", file)
      f.Close()
   }
}

Output

newFile.txt is not writable.

Method 2: Using stat() Method

In this method, we will write a go language program to check if a file has a variable permission or not by using the stat() method present in os package.

Syntax

func Stat(name string) (FileInfo, error)

The Stat() function is present in os package and is used to check the write permission of a file. The function accepts the name of file as an argument and returns a FileInfo structure that contains information about the file, including its permissions.

Algorithm

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

  • Then, start the main() function. Inside the main() store the file whose writable permission is to be checked in a variable.

  • Call the Stat() function of os package and pass the name of the file as argument to the function. Further, store the results in fi and err.

  • Check for errors returned by the Stat() function. If an error is returned, the file does not exist.

  • If no errors are returned, check the FileInfo.Mode() method of the FileInfo structure returned by the os.Stat() function.

  • If the write bit is set in the FileInfo.Mode(), the file is writable. So print the result on the screen by using fmt.Println() function accordingly.

Example

In this example, we are going to use stat() method to check a file has writable permission or not

package main
import (
   "fmt"
   "os"
)

func main() {
   fileName := "notepad.txt"
   fileInfo, err := os.Stat(fileName)
   if err != nil {
      fmt.Printf("Error accessing file %s: %v\n", fileName, err)
      return
   }
   mode := fileInfo.Mode()
   if mode&os.ModePerm == os.ModePerm {
      fmt.Printf("File %s is not writable\n", fileName)
   } else {
      fmt.Printf("File %s is writable\n", fileName)
   }
}

Output

File notepad.txt is writable

Conclusion

We have successfully compiled and executed a go language program to check a file has writable permission or not along with examples. Here we have used two examples, in the first example we are using openFile() function present in os package while in second example we have used Stat() method of the same package.

Updated on: 22-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements