Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Create Temporary Directory

  • TempDir
  • RemoveAll
  • rm -rf
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)
}

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