1
0
mirror of https://github.com/golang/go synced 2024-11-18 08:54:45 -07:00

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: 4c17f9a8e3
GitHub-Pull-Request: golang/go#38379
Reviewed-on: https://go-review.googlesource.com/c/go/+/227868
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Eric 2020-04-13 21:28:47 +00:00 committed by Ian Lance Taylor
parent ca017a6fb9
commit 240eac38e1

View File

@ -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
}