I have a long lived io.Reader
which returns some data every few seconds (never EOF), and a goroutine which does an io.Copy
from that reader to a bytes.Buffer
(also never terminates). Something like so:
var src io.Reader
var buf bytes.Buffer
func main() {
go io.Copy(&buf, src)
// Do stuff. Read from the buffer periodically.
}
What I don't understand is that I see strange results when I try to read from that buffer. It doesn't matter whether I call buf.Bytes()
or ioutil.ReadAll(&buf)
or anything, I just see the first bytes written to the buffer over and over again.
https://play.golang.org/p/yn0JPrvohV
My question is, what am I doing wrong? Can I use bytes.Buffer
in this way (io.Copy
to it and read periodically)?