I need to incrementally append bytes as I receive them to a buffer where I can read them again later. Something like this:
type TwoPassReader struct {
someDataSource SomeDataReader
secondPassReader io.Reader
}
func (r *TwoPassReader) Read(b []byte) (n int, err error) {
n, err := r.someDataSource.Read(b)
if err == nil {
//XXX: this doesn't actually exist, right?
bytesAppended, writeErr := r.secondPassReader.Write(b)
}
return n, err
}
func (r *) ReadSecondPass(b []byte) (n int, err error) {
return r.secondPassReader.Read(b)
}
While the trivial answer for this would be to use bufio.Reader.Peek
, I have a SomeDataReader
object that I can't modify instead of a Reader. Is the best solution to just put the data into a []byte
and return that when ReadSecondPass
is called?