Closed
Description
Filing on behalf of an internal user, who requests that we add convenience wrappers of Compress and Decompress:
func Decompress(b []byte) ([]byte, error) {
rd, err := NewReader(bytes.NewReader(b))
if err != nil {
return nil, err
}
d, err := ioutil.ReadAll(rd)
if cerr := rd.Close(); err == nil {
err = cerr
}
return d, err
}
What are people's thoughts?
Metadata
Metadata
Assignees
Type
Projects
Relationships
Development
No branches or pull requests
Activity
nhooyr commentedon Jul 26, 2016
If we do this, we should use a
sync.Pool
for the readers.bradfitz commentedon Jul 26, 2016
Maybe things have changed, but I would expect pushback for sync.Pool use.
odeke-em commentedon Jul 27, 2016
Just like net/http has:
etc
that underlyingly use the default client's methods perhaps each compress package could have its own Compress and Decompress convenience functions, instead of a default of zlib?
Apologies if am preaching to the choir but it wasn't straight forward for me from your example if the convenience functions would be in the top level compress or in each package.
dsnet commentedon Jul 27, 2016
@odeke-em, the suggestion in the original post is for exactly what you listed out. Thanks for the asking to clarify.
ulikunitz commentedon Jul 29, 2016
If convenience functions are required for compression packages I suggest the alternatives:
These functions would have more use cases and are very easy to use with a bytes.Buffer or a bytes.Reader.
BTW Decompression doesn't need the Close call. It doesn't do anything useful. Depending on the compression package Close for a reader either sets the reader to closed or returns the last returned error if its not io.EOF. IMHO returning a io.ReadCloser for a decompressor is a design error. The bzip2 package does the right thing and returns only an io.Reader.
bradfitz commentedon Sep 12, 2016
Yeah, I don't think helpers which slurp everything to memory are helpful. You're compressing it in the first place because it's large, so pulling it all into memory when it's almost certainly large is not a good idea.
I think the existing Reader- & Writer-based interfaces are the right answer.
Maybe we just need more documentation?
bradfitz commentedon Sep 12, 2016
Sorry, going to close this one. We already have @ulikunitz's
func Decompress
in the form ofioutil.ReadAll
anyway.