Directories
Context
Section titled “Context”Work with directories using os.Mkdir, os.MkdirAll, os.ReadDir, os.Remove, and os.RemoveAll.
Example
Section titled “Example”Create a directory, list its contents, then remove it.
Code example
Section titled “Code example”package main
import ( "fmt" "os")
func main() { err := os.Mkdir("testdir", 0755) if err != nil { fmt.Println("Error creating dir:", err) return } defer os.RemoveAll("testdir")
entries, err := os.ReadDir(".") if err != nil { fmt.Println("Error reading dir:", err) return } for _, e := range entries { fmt.Println(e.Name(), e.IsDir()) }}Output (example)
Section titled “Output (example)”testdir truemain.go false