Skip to main content

Command Palette

Search for a command to run...

Changing file permissions

Updated
1 min readView as Markdown
Changing file permissions
S
DevRel at StackGen | Formerly at Deepfence ,Tenable , Accurics | AWS Community Builder also Docker Community Award Winner at Dockercon2020 | CyberSecurity Innovator of Year 2023 award by Bsides Bangalore | Docker/HashiCorp Meetup Organiser Bangalore & Co-Author of Learn Lightweight Kubernetes with k3s (2019) , Packt Publication & also run Non Profit CloudNativeFolks / CloudSecCorner Community To Empower Free Education reach out me twitterhttps://twitter.com/sangamtwts or just follow on GitHub -> https://github.com/sangam14 for Valuable Resources

how file permissions can be changed programmatically.

Create the filechmod.go file 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())

        }

output:

sangam:golang-daily sangam$ go run filechmod.go
File permissions -rw-r--r--
File permissions -rwxrwxrwx
sangam:golang-daily sangam$

How it works...

  • The Chmod method of the File type in the os package can be used to change file permissions. The preceding example just creates the file and changes the permissions to 0777.

  • Just note that the fi.Mode() is called twice because it extracts the permissions (os.FileMode) for the current state of the file.

  • The shortest way to change the permissions is by using the os.Chmod function, which does the same, but you do not need to obtain the File type in the code.

5 views

GopherLabs

Part 1 of 50

Ultimate Series of Blogs for All Golang Developer

More from this blog

C

CloudNativeFolks Community

140 posts

CloudNativeFolks is non profit community that empower and educate about cloud native technology !