> For the complete documentation index, see [llms.txt](https://ineelhere.gitbook.io/code-with-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ineelhere.gitbook.io/code-with-go/66-temporary-files-and-directories.md).

# Temporary Files and Directories

This Go program demonstrates the usage of temporary files and directories using the `os` and `filepath` packages. Let's go through the code with inline comments and explanations:

```go
// Importing necessary packages.
import (
	"fmt"
	"os"
	"path/filepath"
)

// Function to check and panic if an error occurs.
func check(e error) {
	if e != nil {
		panic(e)
	}
}

// Main function where the execution of the program begins.
func main() {
	// Creating a temporary file with the prefix "sample" in the default temporary directory.
	f, err := os.CreateTemp("", "sample")
	check(err)

	fmt.Println("Temp file name:", f.Name())

	// Defer removing the temporary file at the end of the program.
	defer os.Remove(f.Name())

	// Writing data to the temporary file.
	_, err = f.Write([]byte{1, 2, 3, 4})
	check(err)

	// Creating a temporary directory with the prefix "sampledir" in the default temporary directory.
	dname, err := os.MkdirTemp("", "sampledir")
	check(err)

	fmt.Println("Temp dir name:", dname)

	// Defer removing the temporary directory and its contents at the end of the program.
	defer os.RemoveAll(dname)

	// Creating a file inside the temporary directory and writing data to it.
	fname := filepath.Join(dname, "file1")
	err = os.WriteFile(fname, []byte{1, 2}, 0666)
	check(err)
}
```

#### output

```
Temp file name: ..\..\....\.......\Temp\sample4108738795
Temp dir name: ..\..\....\.......\Temp\sampledir1931724887
```

Explanation:

1. **Creating a Temporary File:**
   * `os.CreateTemp("", "sample")` creates a temporary file in the default temporary directory with the prefix "sample."
   * `defer os.Remove(f.Name())` defers the removal of the temporary file until the end of the program.
2. **Writing Data to the Temporary File:**
   * `f.Write([]byte{1, 2, 3, 4})` writes data to the temporary file.
3. **Creating a Temporary Directory:**
   * `os.MkdirTemp("", "sampledir")` creates a temporary directory in the default temporary directory with the prefix "sampledir."
   * `defer os.RemoveAll(dname)` defers the removal of the temporary directory and its contents until the end of the program.
4. **Creating a File Inside the Temporary Directory:**
   * `filepath.Join(dname, "file1")` constructs the path for a file inside the temporary directory.
   * `os.WriteFile(fname, []byte{1, 2}, 0666)` creates the file and writes data to it.

This program showcases how to create and work with temporary files and directories in Go.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ineelhere.gitbook.io/code-with-go/66-temporary-files-and-directories.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
