file, err := os.Open("sample.txt") if err != nil { log.Fatal(err) } defer file.Close() rs := bufio.NewReader(file) // read first 10 bytes data1 := make([]byte, 10) _, err = rs.Read(data1) if err != nil { log.Fatal(err) } fmt.Println("Read data1:", string(data1)) // seek to beginning of file and read next 5 bytes _, err = rs.Seek(0, io.SeekStart) if err != nil { log.Fatal(err) } data2 := make([]byte, 5) _, err = rs.Read(data2) if err != nil { log.Fatal(err) } fmt.Println("Read data2:", string(data2))
data := []byte("hello world") rs := bytes.NewReader(data) // read first 5 bytes data1 := make([]byte, 5) _, err := rs.Read(data1) if err != nil { log.Fatal(err) } fmt.Println("Read data1:", string(data1)) // seek to index 6 and read next 5 bytes _, err = rs.Seek(6, io.SeekStart) if err != nil { log.Fatal(err) } data2 := make([]byte, 5) _, err = rs.Read(data2) if err != nil { log.Fatal(err) } fmt.Println("Read data2:", string(data2))In this example, we create a `ReadSeeker` from a byte slice using a `bytes.NewReader()`. We then read the first 5 bytes from the slice using the `Read()` method, and seek to index 6 using `Seek()`. Finally, we read the next 5 bytes and print both sets of data. Both of these examples utilize the `ReadSeeker` interface provided by the `io` package in Go.