Changing file permissions
This recipe illustrates how file permissions can be changed programmatically.
How to do it...
- Open the console and create the folder
chapter06/recipe06. - Navigate to the directory.
- Create the
filechmod.gofile with the following content:
package main
import (
"fmt"
"os"
)
func main() {
f, err := os.Create("test.file")
if err != nil {
panic(err)
}
defer f.Close()
// Obtain current permissions
fi, err := f.Stat()
if err != nil {
panic(err)
}
fmt.Printf("File permissions %v\n", fi.Mode())
// Change permissions
err = f.Chmod(0777)
if err != nil {
panic(err)
}
fi, err = f.Stat()
if err != nil {
panic(err)
}
fmt.Printf("File permissions %v\n", fi.Mode())
}- Execute the code by running
go run filechmod...