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

标签 go mkdir

我正在尝试从 Go 可执行文件(例如“dir1/dir2/dir3”)创建一组嵌套目录。我已经成功地用这一行创建了一个目录:

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

但是,我不知道如何在该目录中创建预定的嵌套目录集。

最佳答案

os.Mkdir 用于创建单个目录。要创建文件夹路径,请尝试使用:

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.

编辑:

更新为正确使用 os.ModePerm
对于文件路径的连接,请使用包 path/filepath ,如@Chris 的回答中所述。

关于go - 如何在 Golang 中使用 Mkdir 创建嵌套目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28448543/

相关文章:

PHP警告: mkdir(): Not a directory when creating directory

android - adb shell 命令 : mkdir (for creating a directory)

linux - 为什么 mkdir -p 在 checkinstall 调用的脚本中不能正常工作?

go - Go中的 slice 分配是否复制内存

javascript - 如何将 Golang 中的 [][]byte 发送到浏览器以将其解码为图像

bash - bash 脚本中的 mkdir 错误

php - mkdir 在/tmp 目录下失败

google-app-engine - 是否可以在 Google App Engine 上使用 Go 设置默认错误页面

go - 为什么在 Error() 方法内调用 fmt.Sprint(e) 会导致无限循环?

go找不到sdl