package main import ( "bytes" "fmt" ) func main() { data := []byte("Hello, World!") reader := bytes.NewReader(data) // read from the beginning of the slice buf := make([]byte, 5) n, _ := reader.Read(buf) fmt.Println(string(buf[:n])) // output: "Hello" // read from a specific position within the slice reader.Seek(7, 0) buf = make([]byte, 6) n, _ = reader.Read(buf) fmt.Println(string(buf[:n])) // output: "World!" }
package main import ( "bytes" "fmt" ) func main() { data := []byte("The quick brown fox jumps over the lazy dog.") searchStr := []byte("brown") reader := bytes.NewReader(data) // find position of search string within slice offset := bytes.Index(reader.Bytes(), searchStr) if offset == -1 { panic("String not found") } // seek to position of search string reader.Seek(int64(offset), 0) // read search string and following 5 bytes buf := make([]byte, len(searchStr)+5) n, _ := reader.Read(buf) fmt.Println(string(buf[:n])) // output: "brown fox" }Both of these examples use the "bytes" package library in Go.