How to Create Nested Directories in Go
Created
Modified
Using os.MkdirAll Function
The os.MkdirAll()
function creates a directory named path, along with any necessary parents, and returns nil, or else returns an error.
See the following example:
package main
import (
"os"
)
func main() {
if err := os.MkdirAll("path/subdir", 0750); err != nil && !os.IsExist(err) {
// panic()
}
// create file
}
path/subdir
If path is already a directory, MkdirAll does nothing and returns nil.