How to check file permissions in go

Oct 2, 2018 22:43 · 52 words · 1 minute read

package main

import (
	"fmt"
	"os"
)

func main() {
	var perms = []int{os.O_RDONLY, os.O_WRONLY}
	for perm := range perms {
		file, err := os.OpenFile("myFile.txt", perm, 0666)
		if err != nil {
			if os.IsNotExist(err) {
				panic("File not found.")
			}
			if os.IsPermission(err) {
				fmt.Println(fmt.Sprintf("Error: %v permission denied.", perm))
			}
		}
		file.Close()
	}

}