Creating temporary files and directories

The ioutil package provides two functions: TempDir() and TempFile(). It is the caller's responsibility to delete the temporary items when done. The only benefit these functions provide is that you can pass it an empty string for the directory, and it will automatically create the item in the system's default temporary folder (/tmp on Linux), since the os.TempDir() function will return the default system temporary directory:

package main import ( "fmt" "io/ioutil" "log" "os" ) func main() { // Create a temp dir in the system default temp folder tempDirPath, err := ioutil.TempDir("", "myTempDir") if err != nil { log.Fatal(err) } fmt.Println("Temp dir created:", tempDirPath) // Create a file in new temp ...

Get Security with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.