如何在Golang中创建一个目录?

huangapple go评论88阅读模式
英文:

How to make a directory in Golang?

问题

所以我正在尝试创建一个内容管理系统(CMS),每个用户都可以创建自己的网站,并将其存储在一个以他们选择的名称命名的目录中。然而,当我使用以下代码时:

os.Mkdir("/Users/anonrose/Documents/goCode/src/github.com/anonrose/GoDataStructs/tests/myWebsite", "some permission")

我遇到了关于"some permission"部分的问题。当我尝试访问已创建的目录时,我从来没有正确的权限。在创建目录时,是否有一种os.FileMode可以用来设置读写权限,以便任何人都可以读写该目录?

英文:

So I'm attempting to create a CMS where each user makes their own website and it is stored in a directory named as their choosing. However when I use

os.Mkdir("/Users/anonrose/Documents/goCode/src/github.com/anonrose/GoDataStructs/tests/myWebsite", os."some permission")

the "some permission" part is what I'm having troubles with. When I attempt to access the directory once it's been created I never have the correct permission. Is there a os.FileMode that I can use to set the permissions as read and write for anyone when I go to create the directory.

答案1

得分: 11

如果您需要设置未在这里列出的显式权限位,请使用os.FileMode

os.Mkdir("/path/to/dir", os.FileMode(0522))

uint32的最低有效9位表示文件权限,所以例如0777对应511。

英文:

If you need to set explicit permission bits not listed here then use os.FileMode

os.Mkdir("/path/to/dir", os.FileMode(0522))

The least significant 9 bits of the uint32 represent the file permissions, so 0777 would be 511 for example.

答案2

得分: 1

我总是使用os.ModeDir

package main
import "os"

func main() {
   os.Mkdir("March", os.ModeDir)
}
英文:

I always use os.ModeDir:

package main
import "os"

func main() {
   os.Mkdir("March", os.ModeDir)
}

huangapple
  • 本文由 发表于 2016年3月26日 15:03:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/36232625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定