Create Temporary Directory



examples/tempdir/tempdir.go
package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "os"
)

func main() {
    tempDir, err := ioutil.TempDir("", "demo")
    if err != nil {
        log.Fatal(err)
    }

    defer os.RemoveAll(tempDir)

    fmt.Println(tempDir)
}
The defer os.RemoveAll(tempDir) will make sure the directory is removed when we exit the program.