O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter06/recipe02.
  2. Navigate to the directory.
  3. 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(err)          }          defer os.Remove(tDir)          fmt.Println(tDir)        }
  1. Execute the code by running go run tempfile.go in the main Terminal.
  2. You will see the following output:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required