Standard library / os

os.MkdirAll()

Creates missing directories along a path; succeeds when the target is already a directory. Example uses a disposable temporary workspace. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func MkdirAll(path string, perm FileMode) error

Parameters & Attributes

NameTypeDescription
pathstringPathname interpreted according to this package.
permFileModeRequested permissions before umask when creating.

Return Value

error

Creates missing directories along a path; succeeds when the target is already a directory.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os")
func check(e error) {  if e != nil {    panic(e)  }}func workspace() func() {  root, e := os.MkdirTemp("", "bytemind-go-")  check(e)  old, e := os.Getwd()  check(e)  check(os.Chdir(root))  return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() {  defer workspace()()  check(os.MkdirAll("a/b", 0700))  i, e := os.Stat("a/b")  check(e)  fmt.Println(i.IsDir())}
Go Reference