package main import ( "bytes" "fmt" ) func main() { data := []byte{0x01, 0x02, 0x03, 0x04, 0x05} chunkSize := 2 reader := bytes.NewReader(data) for reader.Len() > 0 { chunk := make([]byte, chunkSize) _, err := reader.Read(chunk) if err != nil { panic(err) } fmt.Println(chunk) } }
package main import ( "bytes" "fmt" ) func main() { data := []byte{0x01, 0x02, 0x03, 0x04, 0x05} reader := bytes.NewReader(data) for reader.Len() > 0 { b, err := reader.ReadByte() if err != nil { panic(err) } fmt.Println(b) } }This code reads data from `data` using a `bytes.Reader` and the `ReadByte()` method. The `Len()` method is used to check if there is any data remaining to be read. The `ReadByte()` method is called repeatedly to read the data byte by byte and then print it. The package library used in these code examples is the `bytes` package of Go.