Joe Tsai has posted comments on this change.
compress/gzip: add examples
Patch Set 1:
(4 comments)
https://go-review.googlesource.com/#/c/29218/1/src/compress/gzip/example_test.go
File src/compress/gzip/example_test.go:
Line 15: func ExampleNewWriter() {
I would change this into a package-level example and use NewWriter and
NewReader in the same example function.
Also, I think this example should make use of gzip.Header. The Name,
ModTime, and Comment are probably the most frequently used header fields.
Keep in mind that the implementation is inconsistent in that the Reader
reads the header in NewReader or Reset, and the Writer writes the header at
the first Write or Close.
Line 17: defer wc.Close()
Since people have a tendency to copy-paste example code, we should be
consistent about error handling.
Both Close and Write return errors, so we should check them. Can you add
error checking here and all other cases?
PS1, Line 46: 31, 139, 8, 0, 0, 9, 110, 136, 0, 255, 202, 72, 205, 201,
201, 215, 81, 40, 207, 47, 202, 73, 225, 2, 4, 0, 0, 255, 255, 83, 116, 36,
244, 13, 0, 0, 0
I'm not particularly fond of seeing hard-coded gzip data in the example. If
possible, we should craft the examples in such a way that this doesn't
show. My suggestion about Multistream below should avoid this.
PS1, Line 33: func ExampleWriter_Reset() {
: var buf bytes.Buffer
: wc := gzip.NewWriter(&buf)
: defer wc.Close()
:
: wc.Write([]byte("hello, world\n"))
:
: wc.Reset(os.Stdout)
:
: wc.Write([]byte("Gophers\n"))
: }
:
: func ExampleReader_Reset() {
: buf1 := []byte{31, 139, 8, 0, 0, 9, 110, 136, 0, 255, 202,
72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 225, 2, 4, 0, 0, 255,
255, 83, 116, 36, 244, 13, 0, 0, 0}
:
: rc, err := gzip.NewReader(bytes.NewReader(buf1))
: if err != nil {
: log.Fatal(err)
: }
: defer rc.Close()
:
: io.Copy(os.Stdout, rc)
:
: buf2 := []byte{31, 139, 8, 0, 0, 9, 110, 136, 0, 255, 114,
207, 47, 200, 72, 45, 42, 230, 2, 4, 0, 0, 255, 255, 248, 97, 174, 38, 8,
0, 0, 0}
: b2 := bytes.NewReader(buf2)
: if err := rc.Reset(b2); err != nil {
: log.Fatal(err)
: }
:
: io.Copy(os.Stdout, rc)
: }
Use of Reset is fairly straightforward in gzip. Instead of providing
examples for Reset, I would suggest providing an example for
Reader.Multistream, which is a clunky API to use.
In constructing a multistream gzip, you could probably make use of
Writer.Reset and kill 2 birds with one stone.
Gerrit-HasComments: Yes