> 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/61-reading-files.md).

# Reading Files

This Go code demonstrates reading a file using various methods from the `os` and `io` packages. Let's go through the code with inline comments and explanations:

```go
// Importing necessary packages.
import (
	"bufio"
	"fmt"
	"io"
	"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() {
	// Reading the entire contents of the file using os.ReadFile.
	dat, err := os.ReadFile("/tmp/dat")
	check(err)
	fmt.Print(string(dat))

	// Opening the file for further reading.
	f, err := os.Open("/tmp/dat")
	check(err)

	// Reading the first 5 bytes from the file.
	b1 := make([]byte, 5)
	n1, err := f.Read(b1)
	check(err)
	fmt.Printf("%d bytes: %s\n", n1, string(b1[:n1]))

	// Seeking to a specific position (offset 6) in the file and reading 2 bytes.
	o2, err := f.Seek(6, 0)
	check(err)
	b2 := make([]byte, 2)
	n2, err := f.Read(b2)
	check(err)
	fmt.Printf("%d bytes @ %d: %v\n", n2, o2, string(b2[:n2]))

	// Performing another seek and reading at least 2 bytes into a buffer.
	o3, err := f.Seek(6, 0)
	check(err)
	b3 := make([]byte, 2)
	n3, err := io.ReadAtLeast(f, b3, 2)
	check(err)
	fmt.Printf("%d bytes @ %d: %s\n", n3, o3, string(b3))

	// Seeking back to the beginning of the file.
	_, err = f.Seek(0, 0)
	check(err)

	// Using a buffered reader to peek at the first 5 bytes without consuming them.
	r4 := bufio.NewReader(f)
	b4, err := r4.Peek(5)
	check(err)
	fmt.Printf("5 bytes: %s\n", string(b4))

	// Closing the file.
	f.Close()
}
```

#### output

* First make sure you have the dummy files on your working directory.
* You might need to change some code for that.
* Then run the `.go` file with the above code.

```
hello
go
5 bytes: hello
2 bytes @ 6: go
2 bytes @ 6: go
5 bytes: hello
```

Explanation:

1. **Reading Entire File:**
   * `os.ReadFile("/tmp/dat")` reads the entire contents of the file into a byte slice.
2. **Reading Specific Bytes:**
   * `os.Open("/tmp/dat")` opens the file for further reading.
   * `f.Read(b1)` reads the first 5 bytes from the file.
3. **Seeking and Reading:**
   * `f.Seek(6, 0)` seeks to the 6th byte in the file.
   * `f.Read(b2)` reads 2 bytes from the current position.
4. **Seeking, Reading At Least, and Buffered Reading:**
   * `f.Seek(6, 0)` seeks to the 6th byte again.
   * `io.ReadAtLeast(f, b3, 2)` reads at least 2 bytes into a buffer.
5. **Peeking with Buffered Reader:**
   * `bufio.NewReader(f)` creates a buffered reader.
   * `r4.Peek(5)` peeks at the first 5 bytes without consuming them.
6. **Closing the File:**
   * `f.Close()` closes the file.

This code demonstrates various ways to read from a file in Go using different methods from the `os` and `io` packages.


---

# 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/61-reading-files.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.
