package main import ( "bytes" "fmt" ) func main() { str := "hello world" reader := bytes.NewReader([]byte(str)) data := make([]byte, 5) n, err := reader.Read(data) if err != nil { panic(err) } fmt.Println(data[:n]) // prints "hello" }
package main import ( "bytes" "fmt" "io/ioutil" ) func main() { fileBytes, err := ioutil.ReadFile("example.txt") if err != nil { panic(err) } reader := bytes.NewReader(fileBytes) data := make([]byte, 10) n, err := reader.Read(data) if err != nil { panic(err) } fmt.Println(data[:n]) }In this example, we first read the contents of a file "example.txt" using ioutil.ReadFile function. We pass the file content to bytes.NewReader to create a Reader instance. We then read the first 10 bytes from the reader and print the result. Conclusion: The bytes Reader package is a part of the standard library in Go. It allows users to read data from byte sequences such as files and strings. It is a useful package for when we want to manipulate data in a byte sequence.