Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between io.TeeReader and io.Copy?

Tags:

io

go

io.TeeReader and io.Copy both read from a reader and write into a writer. What is the difference?

like image 822
CZ G Avatar asked Oct 22 '25 19:10

CZ G


1 Answers

io.Copy() copies data from a source io.Reader to a destination io.Writer. And that's all. You don't get the data that was copied, it's not returned to you.

io.TeeReader() on the other hand does not perform the copy automatically. It just returns you an io.Reader which if you read from, the data you get will also be written to the io.Writer you pass to io.TeeReader().

io.TeeReader() is useful if you need to copy data from a reader to a writer, but you also need the data because you want to inspect it or perform calculations on it.

For example let's say you want to copy an io.Reader to the standard output, but you also want to calculate the MD5 hash of the copied content. You may do it like this:

s := "Hello World"

r := io.TeeReader(strings.NewReader(s), os.Stdout)

h := md5.New()
if _, err := io.Copy(h, r); err != nil {
    panic(err)
}

fmt.Printf("\nHash: %x", h.Sum(nil))

This will output (try it on the Go Playground):

Hello World
Hash: b10a8db164e0754105b7a99be72e3fe5

Note that this can also be achieved using io.MultiWriter() instead of io.TeeReader():

s := "Hello World"

h := md5.New()
mw := io.MultiWriter(h, os.Stdout)
if _, err := io.Copy(mw, strings.NewReader(s)); err != nil {
    panic(err)
}

fmt.Printf("\nHash: %x", h.Sum(nil))

This outputs the same. Try this one on the Go Playground.

like image 117
icza Avatar answered Oct 24 '25 13:10

icza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!