dourong8495 2015-02-11 07:15
浏览 191
已采纳

如何在Golang中使用Mkdir创建嵌套目录?

I am trying to create a set of nested directories from a Go executable such as 'dir1/dir2/dir3'. I have succeeded in creating a single directory with this line:

os.Mkdir("." + string(filepath.Separator) + c.Args().First(),0777);

However, I have no idea how to approach creating a predetermined nested set of directories inside of that directory.

  • 写回答

5条回答 默认 最新

  • doue1925 2015-02-11 07:25
    关注

    os.Mkdir is used to create a single directory. To create a folder path, instead try using:

    os.MkdirAll(folderPath, os.ModePerm)
    

    Go documentation

    func MkdirAll(path string, perm FileMode) error

    MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. The permission bits perm are used for all directories that MkdirAll creates. If path is already a directory, MkdirAll does nothing and returns nil.

    Edit:

    Updated to correctly use os.ModePerm instead.
    For concatenation of file paths, use package path/filepath as described in @Chris' answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog