Go Tidbit: Peek a Snippet From io.Reader
February 5, 2023
#100DaysToOffload
#Go
#Tidbit
You have an io.Reader, and you want to extract a small snippet from the beginning of the io.Reader and then put it back.
Ideally, io.ReadSeeker would let you do that. But not all io.Readers can seek.
And, like toothpaste from its tube, you cannot put something back once you read it from an io.Reader. But you can do the next best thing:
| |
As you can see, you can use io.LimitReader to read the first n bytes. Then use io.MultiReader to combine those bytes with the remainder of data in r.
This post is 14th of my #100DaysToOffload challenge. Want to get involved? Find out more at 100daystooffload.com.
comments powered by Disqus
Similar Blog Posts
February 5, 2023
#100DaysToOffload
#Go
#Tidbit