Welcome To Golang By Example

Change folder/directory permissions in Go (Golang)

os.Chmod() function can be used to change the permissions of an existing folder or directory. Below is the signature of the function

func Chmod(name string, mode FileMode) error

Code

package main

import (
    "fmt"
    "log"
    "os"
)

func main() {
    err := os.Mkdir("new", 0755)
    if err != nil {
        log.Fatal(err)
    }
    stats, err := os.Stat("new")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Permission Folder Before: %s\n", stats.Mode())
    err = os.Chmod("new", 0700)
    if err != nil {
        log.Fatal(err)
    }
    stats, err = os.Stat("new")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Permission Folder After:  %s\n", stats.Mode())
}

Output:

Permission Folder Before: drwxr-xr-x
Permission Folder After:  drwx------