Search code examples
filegodirectorypermissions

GoLang os.Chdir() permission denied


I am writing a program that creates a directory and then changes the working directory to the newly created directorty in order to do some work:

func main() {
    err := os.Mkdir("English", 0777) // I know 777 is not good practice, first I want to get Chdir() working
    if err != nil && !os.IsExist(err) {
        log.Fatal(err)
    }
    err = os.Chdir("English")
    if err != nil {
        log.Fatal(err)
    }
}

Console output:

023/02/05 18:15:45 chdir English: permission denied
exit status 1

Solution

  • Simple fix: executing the program using sudo resulted in the directoy being created with permissions as specified.