anonrose
anonrose

Reputation: 1381

How to make a directory in Golang?

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.

Upvotes: 9

Views: 10472

Answers (2)

Zombo
Zombo

Reputation: 1

I always use os.ModeDir:

package main
import "os"

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

Upvotes: 1

sberry
sberry

Reputation: 131998

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.

Upvotes: 12

Related Questions