How to create nested directories using Mkdir in Golang?

前端 未结 5 1221
野趣味
野趣味 2021-01-30 15:33

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:



        
相关标签:
5条回答
  • 2021-01-30 15:56

    This way you don't have to use any magic numbers:

    os.MkdirAll(newPath, os.ModePerm)
    

    Also, rather than using + to create paths, you can use:

    import "path/filepath"
    path := filepath.Join(someRootPath, someSubPath)
    

    The above uses the correct separators automatically on each platform for you.

    0 讨论(0)
  • 2021-01-30 16:00

    An utility method like the following can be used to solve this.

    import (
      "os"
      "path/filepath"
      "log"
    )
    
    func ensureDir(fileName string) {
      dirName := filepath.Dir(fileName)
      if _, serr := os.Stat(dirName); serr != nil {
        merr := os.MkdirAll(dirName, os.ModePerm)
        if merr != nil {
            panic(merr)
        }
      }
    }
    
    
    
    func main() {
      _, cerr := os.Create("a/b/c/d.txt")
      if cerr != nil {
        log.Fatal("error creating a/b/c", cerr)
      }
      log.Println("created file in a sub-directory.")
    }
    
    0 讨论(0)
  • 2021-01-30 16:03

    If the issue is to create all the necessary parent directories, you could consider using os.MkDirAll()

    MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error.

    The path_test.go is a good illustration on how to use it:

    func TestMkdirAll(t *testing.T) {
        tmpDir := TempDir()
        path := tmpDir + "/_TestMkdirAll_/dir/./dir2"
        err := MkdirAll(path, 0777)
        if err != nil {
        t.Fatalf("MkdirAll %q: %s", path, err)
        }
        defer RemoveAll(tmpDir + "/_TestMkdirAll_")
    ...
    }
    

    (Make sure to specify a sensible permission value, as mentioned in this answer)

    0 讨论(0)
  • 2021-01-30 16:05

    This is one alternative for achieving the same but it avoids race condition caused by having two distinct "check ..and.. create" operations.

    package main
    
    import (
        "fmt"
        "os"
    )
    
    func main()  {
        if err := ensureDir("/test-dir"); err != nil {
            fmt.Println("Directory creation failed with error: " + err.Error())
            os.Exit(1)
        }
        // Proceed forward
    }
    
    func ensureDir(dirName string) error {
    
        err := os.MkdirAll(dirName, os.ModeDir)
    
        if err == nil || os.IsExist(err) {
            return nil
        } else {
            return err
        }
    }
    
    0 讨论(0)
  • 2021-01-30 16:11

    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.

    0 讨论(0)
提交回复
热议问题