Home >Backend Development >Golang >GoLang os.Chdir() permission denied

GoLang os.Chdir() permission denied

WBOY
WBOYforward
2024-02-06 09:24:13432browse

GoLang os.Chdir() 权限被拒绝

Question content

I'm writing a program that creates a directory and then changes the working directory to the newly created directory to perform 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

Correct answer


Simple fix: Executing the program with sudo causes the directory to be created with the specified permissions.

The above is the detailed content of GoLang os.Chdir() permission denied. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete