# Writing Files

This Go code demonstrates writing to files using various methods from the `os` and `bufio` packages. Let's go through the code with inline comments and explanations:

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

// Function to check for errors and panic if an error is encountered.
func check(e error) {
	if e != nil {
		panic(e)
	}
}

// The main function, where the execution of the program begins.
func main() {
	// Writing a byte slice to a file using os.WriteFile.
	d1 := []byte("hello\ngo\n")
	err := os.WriteFile("/tmp/dat1", d1, 0644)
	check(err)

	// Creating a new file and writing bytes to it using os.Create.
	f, err := os.Create("/tmp/dat2")
	check(err)
	defer f.Close() // Ensuring the file is closed when the function exits.

	// Writing a byte slice to the file.
	d2 := []byte{115, 111, 109, 101, 10}
	n2, err := f.Write(d2)
	check(err)
	fmt.Printf("wrote %d bytes\n", n2)

	// Writing a string to the file using f.WriteString.
	n3, err := f.WriteString("writes\n")
	check(err)
	fmt.Printf("wrote %d bytes\n", n3)

	// Ensuring that all changes to the file are flushed to disk.
	f.Sync()

	// Creating a buffered writer for efficient writing.
	w := bufio.NewWriter(f)
	n4, err := w.WriteString("buffered\n")
	check(err)
	fmt.Printf("wrote %d bytes\n", n4)

	// Flushing the buffered writer to ensure all data is written to the file.
	w.Flush()
}
```

#### Output

```
wrote 5 bytes
wrote 7 bytes
wrote 9 bytes
```

Explanation:

1. **Writing to Files:**
   * `os.WriteFile("/tmp/dat1", d1, 0644)` writes the byte slice `d1` to a file named "dat1".
   * `os.Create("/tmp/dat2")` creates a new file or truncates an existing one.
2. **Writing Bytes and Strings:**
   * `f.Write(d2)` writes the byte slice `d2` to the file.
   * `f.WriteString("writes\n")` writes the string "writes" to the file.
3. **Flushing and Syncing:**
   * `f.Sync()` ensures that all changes to the file are flushed to disk.
4. **Buffered Writing:**
   * `bufio.NewWriter(f)` creates a buffered writer for efficient writing.
   * `w.WriteString("buffered\n")` writes the string "buffered" using the buffered writer.
   * `w.Flush()` ensures that all data buffered in the writer is written to the file.

This code demonstrates different methods for writing data to files in Go, including direct writing, buffered writing, and flushing changes to disk.


---

# Agent Instructions: 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:

```
GET https://ineelhere.gitbook.io/code-with-go/62-writing-files.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
