Besides the other answers, remember that on Unix and Linux style operating systems, all programs run with a umask setting. The umask, which in many cases defaults to 022 or sometimes 002, is the set of permissions that the system will automatically remove from file and directory creation requests.
What this means is that most programs–there are several exceptions to this rule—should use mode 0666
for creating files and mode 0777
for creating directories. The user's configuration, recorded in the running process, says which of these permissions to take away. If the user's setting is 022
, and we create a file with mode 0666
, the actual setting we get is rw-r--r--
: read and write for the user, read-only for the group, and read-only for others.
If a user wishes to extend writability to their group, they need only set their umask to 2
: now they take away write permission for others, but leave it for their group. New files are now created with mode rw-rw-r--
. The program does not change: it still uses 0666
for its mode. But the files are created with mode 0664
.
Similarly, if you call os.Mkdir
or os.MkdirAll
with 0777
, the umask will take away the unwanted permissions, leaving you with the right permissions.
But I mentioned that there are exceptions. These include programs that make copies of sensitive information meant only for the user: these should generally use mode 0700
for directories and 0600
for files. They may include long-running servers that act as a system user rather than any one individual ... although those servers could be run with a correct umask, in which case, 0777
or 0666
is fine.
You must apply some judgment here. Programs that are especially security-conscious, such as ssh or similar, may wish to use limited permissions, and may even want to check (with os.Lstat
or similar) that permissions are appropriately tight on important directories.
(Note that the umask does not apply to os.Chmod
calls. Here you choose the mode directly.)