Standard library / os
os.Mkdir()
Creates one directory; parent directories must exist. Requested permissions are filtered by the process umask. Example uses a disposable temporary workspace. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func Mkdir(name string, perm FileMode) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Name or pathname of the target. |
| perm | FileMode | Requested permissions before umask when creating. |
Return Value
error
Creates one directory; parent directories must exist.
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.Mkdir("docs", 0700)) i, e := os.Stat("docs") check(e) fmt.Println(i.IsDir())}