Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a directory in Golang?

Tags:

go

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.

like image 390
anonrose Avatar asked Mar 26 '16 07:03

anonrose


People also ask

How do I create a directory in Golang?

To create a single directory in Go, use the os. Mkdir() function. If you want to create a hierarchy of folders (nested directories), use os. MkdirAll() .

What is the go directory?

GOPATH is a variable that defines the root of your workspace. By default, the workspace directory is a directory that is named go within your user home directory (~/go for Linux and MacOS, %USERPROFILE%/go for Windows). GOPATH stores your code base and all the files that are necessary for your development.

How do I change directory in Golang?

os. Chdir() is used to change the current working directory to the named directory in golang. It is similar to the cd command.


1 Answers

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.

like image 172
sberry Avatar answered Nov 14 '22 12:11

sberry