8

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.

anonrose
  • 1,271
  • 3
  • 12
  • 19
  • Have you tried `os.Mkdir("/Users/anonrose/Documents/goCode/src/github.com/anonrose/GoDataStructs/tests/myWebsite", int(0777))`? – Ruslan Mar 26 '16 at 07:08
  • Note the leading 0 in `0777` to make the value octal, just `777` will give strange permissions. – Joachim Isaksson Mar 26 '16 at 07:12
  • @JoachimIsaksson that didn't but I just used `os.ModePerm` which is of type FileMode so that got it done. Thank you. – anonrose Mar 26 '16 at 07:17
  • Seems like a duplicated question of [os.MkDir and os.MkDirAll permission value?](http://stackoverflow.com/questions/14249467/os-mkdir-and-os-mkdirall-permission-value) – Hang Mar 26 '16 at 07:37

2 Answers2

11

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.

sberry
  • 128,281
  • 18
  • 138
  • 165
1

I always use os.ModeDir:

package main
import "os"

func main() {
   os.Mkdir("March", os.ModeDir)
}
Zombo
  • 1
  • 62
  • 391
  • 407