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
Reading (for example) the doc about NewWriter it is not clear nor trivial to understand how gzip.NewWriter could interact with (for example) bufio.NewWriter. I found this gist that helped me in understanding the right order.
May be an example like
package main
import (
"bufio""compress/gzip""log""os"
)
funcmain() {
file, err:=os.Create("File.gz")
iferr!=nil {
log.Fatal(err)
}
deferfile.Close()
gzipWriter:=gzip.NewWriter(file)
defergzipWriter.Close()
defergzipWriter.Flush()
writer:=bufio.NewWriter(gzipWriter)
deferwriter.Flush()
writer.WriteString("gzipWriter.go created this file.\n")
}
can help.
The text was updated successfully, but these errors were encountered:
But that's the wrong order (buffer -> gzip -> file). You typically want to buffer writes to the file (gzip -> buffer -> file), not writes to the gzip compressor (it doesn't care how big or small your writes are). Also you shouldn't ignore the errors from Flush and Close.
What I'm saying is: I agree this should be better documented. I don't think compress/gzip is the right place to do it, though.
I think we should add a solid section to the tour about working with the io interfaces. Or even a blog post would do.
Yes, I missed the errors!! Ops!!
But what is the best way to check for the errors with defer???
I thought gzip was like a filter, compressing whatever bufio.Writer passes when it does it.
Like an extra layer between the writer and the file object compressing the data. It seems I was wrong, but how can I deal with an application that can write both normal and compressed files depending on the input? If gzip.Writer sits on top of bufio, I need to change the writer I pass to the function that actually writes when I change the output file type. Or not?
Regarding the docs, my opinion is that some examples should be written both in a tutorial (blog/tour) and in the docs (maybe something shorted/simpler). But this is only my opinion. At least, maybe a link to the tutorial placed in the docs page would help.
Submit here having read the docs thread on the ML.
Reading (for example) the doc about
NewWriter
it is not clear nor trivial to understand howgzip.NewWriter
could interact with (for example)bufio.NewWriter
. I found this gist that helped me in understanding the right order.May be an example like
can help.
The text was updated successfully, but these errors were encountered: