September 2017
Intermediate to advanced
466 pages
9h 33m
English
With the help of the ls(1) command, you can find out the permissions of a file:
$ ls -l /bin/ls -rwxr-xr-x 1 root wheel 38624 Mar 23 01:57 /bin/ls
In this subsection, we will look at how to print the permissions of a file or directory using Go: the Go code will be saved in permissions.go and will be presented in two parts. The first part is as follows:
package main
import (
"fmt"
"os"
)
func main() {
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide an argument!")
os.Exit(1)
}
file := arguments[1]
The second part contains the important Go code:
info, err := os.Stat(file) if err != nil { fmt.Println("Error:", err) os.Exit(1) } mode := info.Mode() fmt.Print(file, ...Read now
Unlock full access