From 240eac38e1be22d8dfaf96731c337afa479248ff Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 13 Apr 2020 21:28:47 +0000 Subject: [PATCH] io: simplify Examples - CopyN: 5 creates ambiguity with respect to whitespace and upperbound - TeeReader less boilerplate and displays a common usage of it - SectionReader_* all sections unified to 5:17 for clarity - SectionReader_Seek uses io.Copy to stdout like other examples - Seeker_Seek remove useless prints - Pipe print reader like other examples Updates #36417 Change-Id: Ibd01761d5a5786cdb1ea934f7a98f8302430c8a5 GitHub-Last-Rev: 4c17f9a8e32d89743b7eaec7c52032256972cc0a GitHub-Pull-Request: golang/go#38379 Reviewed-on: https://go-review.googlesource.com/c/go/+/227868 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/io/example_test.go | 47 ++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/src/io/example_test.go b/src/io/example_test.go index 2eaab678c9..465eed4d5e 100644 --- a/src/io/example_test.go +++ b/src/io/example_test.go @@ -48,7 +48,7 @@ func ExampleCopyBuffer() { func ExampleCopyN() { r := strings.NewReader("some io.Reader stream to be read") - if _, err := io.CopyN(os.Stdout, r, 5); err != nil { + if _, err := io.CopyN(os.Stdout, r, 4); err != nil { log.Fatal(err) } @@ -136,25 +136,15 @@ func ExampleMultiReader() { } func ExampleTeeReader() { - r := strings.NewReader("some io.Reader stream to be read\n") - var buf bytes.Buffer - tee := io.TeeReader(r, &buf) + var r io.Reader = strings.NewReader("some io.Reader stream to be read\n") - printall := func(r io.Reader) { - b, err := ioutil.ReadAll(r) - if err != nil { - log.Fatal(err) - } + r = io.TeeReader(r, os.Stdout) - fmt.Printf("%s", b) - } - - printall(tee) - printall(&buf) + // Everything read from r will be copied to stdout. + ioutil.ReadAll(r) // Output: // some io.Reader stream to be read - // some io.Reader stream to be read } func ExampleSectionReader() { @@ -171,7 +161,7 @@ func ExampleSectionReader() { func ExampleSectionReader_ReadAt() { r := strings.NewReader("some io.Reader stream to be read\n") - s := io.NewSectionReader(r, 5, 16) + s := io.NewSectionReader(r, 5, 17) buf := make([]byte, 6) if _, err := s.ReadAt(buf, 10); err != nil { @@ -186,30 +176,24 @@ func ExampleSectionReader_ReadAt() { func ExampleSectionReader_Seek() { r := strings.NewReader("some io.Reader stream to be read\n") - s := io.NewSectionReader(r, 5, 16) + s := io.NewSectionReader(r, 5, 17) if _, err := s.Seek(10, io.SeekStart); err != nil { log.Fatal(err) } - buf := make([]byte, 6) - if _, err := s.Read(buf); err != nil { + if _, err := io.Copy(os.Stdout, s); err != nil { log.Fatal(err) } - fmt.Printf("%s\n", buf) - // Output: // stream } func ExampleSeeker_Seek() { r := strings.NewReader("some io.Reader stream to be read\n") - if _, err := io.Copy(os.Stdout, r); err != nil { - log.Fatal(err) - } - r.Seek(15, io.SeekStart) + r.Seek(5, io.SeekStart) // move to the 5th char from the start if _, err := io.Copy(os.Stdout, r); err != nil { log.Fatal(err) } @@ -220,8 +204,7 @@ func ExampleSeeker_Seek() { } // Output: - // some io.Reader stream to be read - // stream to be read + // io.Reader stream to be read // read } @@ -247,14 +230,14 @@ func ExamplePipe() { r, w := io.Pipe() go func() { - fmt.Fprint(w, "some text to be read\n") + fmt.Fprint(w, "some io.Reader stream to be read\n") w.Close() }() - buf := new(bytes.Buffer) - buf.ReadFrom(r) - fmt.Print(buf.String()) + if _, err := io.Copy(os.Stdout, r); err != nil { + log.Fatal(err) + } // Output: - // some text to be read + // some io.Reader stream to be read }