Go Tidbit: How Much Was Read From io.Reader?
You have an io.Reader
, and you are about to pass it to some utility that will read from it. But the utility won’t tell you how much it has read from the io.Reader
.
How do you figure out how many bytes of data were in the io.Reader
?
Like this snippet of Go+AWS code:
|
|
You could implement a type that wraps the io.Reader
and proxies all calls to Read
while keeping track of the number of bytes read.
Or, you could use an io.LimitedReader
:
|
|
This method works while the number of bytes in the io.Reader
is not greater than the maximum possible value of a 64-bit integer. You probably aren’t hitting that limit. If you are, you have other challenges to deal with.
This post is 13th of my #100DaysToOffload challenge. Want to get involved? Find out more at 100daystooffload.com.
comments powered by Disqus