Creating temporary files
Temporary files are commonly used while running test cases or if your application needs to have a place to store short-term content such as user data uploads and currently processed data. This recipe will present the easiest way to create such a file or directory.
How to do it...
- Open the console and create the folder
chapter06/recipe02
. - Navigate to the directory.
- Create the
tempfile.go
file with the following content:
package main import "io/ioutil" import "os" import "fmt" func main() { tFile, err := ioutil.TempFile("", "gostdcookbook") if err != nil { panic(err) } // The called is responsible for handling // the clean up. defer os.Remove(tFile.Name()) fmt.Println(tFile.Name()) // TempDir returns // the path in string. tDir, err := ioutil.TempDir("", "gostdcookbookdir") if err != nil { panic...