dtmwnqng38644 2017-01-10 09:18
浏览 380
已采纳

设置os.Mkdir权限

I'm trying to create directories with certain permissions using os.Mkdir but I cannot make it work, for some reason.

My test program is:

package main

import (
        "log"
        "os"
)

func main() {                 
    err := os.Mkdir("testdir", 0775)
    if err != nil {              
        log.Print(err)
    }
}

However, the created directory has the default 0755 permissions:

drwxr-xr-x 2 user user 4096 Jan 10 10:14 testdir

A chmod from the shell works just fine, so I'm not sure why the Go program is not working.

  • 写回答

1条回答 默认 最新

  • duandao6414 2017-01-10 09:35
    关注

    When creating a file, Unix-like system use a permission mask (umask) to create the default permissions.

    With a umask value of 0022, new directories will be created with permissions 0755 at most. New files will have permissions 0644 at most.

    If you want to create a new directory with permissions 0775, then you have to set your umask value to 0002.

    An other way of working this around is to modify the permissions after creating the file : Create it with default permissions with os.Mkdir, then modify those permissions with os.Chmod.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?