You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to return a tar.Reader from a function, but its Read() method has a strange behavior
if you write a tar using tar.Writer interface it points to a buffer, trying to create a reader with tar.NewReader(buf) gives the correct list of files from tr.Next() method, but its Read() method always points to end of buffer
Here's an example: https://play.golang.org/p/GnCF_B1TWFE
here error is EOF, although we can iterate over tr.Next() subsequently (aren't readers streams?)
We could make sure this isn't about allocation using ioutil.ReadAll(): https://play.golang.org/p/DLFn_J0pgzN
here error is nil, but the []byte is empty
Things get a little more weird if you edit first example adding a Next() call before calling tr.Read() https://play.golang.org/p/RYJY1cz02Ms
Now []byte is not empty but instead has the first file content, and read error: EOF 🤯
What did you expect to see?
A filled []byte when read is called
What did you see instead?
An empty []byte in return
The text was updated successfully, but these errors were encountered:
After calling tar.NewReader, you need to immediately call Next to advance to the first file (and get any error occurred while parsing the header of the first file -- eg: when reading a file which is not a tar). This first Next call parses the tar header and decodes information on the first file, so that it is then possible to start reading the first file.
This is (lightly) documented on type Reader when it says: Reader.Next advances to the next file in the archive (including the first), and then Reader can be treated as an io.Reader to access the file's data..
Yes. After you read the first file, call Next again to go to the second one. This is all documented and the examples in the documentations show you how to decode all the contents using a loop.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I'm trying to return a tar.Reader from a function, but its
Read()
method has a strange behaviorif you write a tar using
tar.Writer
interface it points to a buffer, trying to create a reader withtar.NewReader(buf)
gives the correct list of files fromtr.Next()
method, but itsRead()
method always points to end of bufferHere's an example:
https://play.golang.org/p/GnCF_B1TWFE
here error is EOF, although we can iterate over
tr.Next()
subsequently (aren't readers streams?)We could make sure this isn't about allocation using
ioutil.ReadAll()
:https://play.golang.org/p/DLFn_J0pgzN
here error is nil, but the []byte is empty
Things get a little more weird if you edit first example adding a
Next()
call before callingtr.Read()
https://play.golang.org/p/RYJY1cz02Ms
Now []byte is not empty but instead has the first file content, and read error: EOF 🤯
What did you expect to see?
A filled []byte when read is called
What did you see instead?
An empty []byte in return
The text was updated successfully, but these errors were encountered: