Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a directory with normal permission in go

Tags:

go

How can I create a directory with normal permissions (say 0700 in octal notation) with the os.Mkdir method. I did not manage to find how to set the perm value correctly.

like image 384
user983716 Avatar asked Feb 13 '23 07:02

user983716


1 Answers

You can use that octal notation directly:

os.Mkdir("dirname", 0700)

From the documentation for FileMode:

The nine least-significant bits are the standard Unix rwxrwxrwx permissions

The mode bits are defined so that you may use normal octal notation just as you would with chmod. However, you must prefix it with a zero to tell Go it is an octal literal.

Also, remember that the 4th number doesn't exist like it does in chmod. With chmod, you could do 1700 to set the sticky bit. In Go, you would need to set the bit defined in the OS lib by doing something like: 0700 | os.ModeSticky

like image 181
Stephen Weinberg Avatar answered Mar 07 '23 19:03

Stephen Weinberg