dtmwnqng38644 2017-01-10 01: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 01: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部