mirror of
https://github.com/golang/go
synced 2024-11-18 11:04:42 -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:
parent
ca017a6fb9
commit
240eac38e1
@ -48,7 +48,7 @@ func ExampleCopyBuffer() {
|
|||||||
func ExampleCopyN() {
|
func ExampleCopyN() {
|
||||||
r := strings.NewReader("some io.Reader stream to be read")
|
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)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,25 +136,15 @@ func ExampleMultiReader() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ExampleTeeReader() {
|
func ExampleTeeReader() {
|
||||||
r := strings.NewReader("some io.Reader stream to be read\n")
|
var r io.Reader = strings.NewReader("some io.Reader stream to be read\n")
|
||||||
var buf bytes.Buffer
|
|
||||||
tee := io.TeeReader(r, &buf)
|
|
||||||
|
|
||||||
printall := func(r io.Reader) {
|
r = io.TeeReader(r, os.Stdout)
|
||||||
b, err := ioutil.ReadAll(r)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("%s", b)
|
// Everything read from r will be copied to stdout.
|
||||||
}
|
ioutil.ReadAll(r)
|
||||||
|
|
||||||
printall(tee)
|
|
||||||
printall(&buf)
|
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// some io.Reader stream to be read
|
// some io.Reader stream to be read
|
||||||
// some io.Reader stream to be read
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleSectionReader() {
|
func ExampleSectionReader() {
|
||||||
@ -171,7 +161,7 @@ func ExampleSectionReader() {
|
|||||||
|
|
||||||
func ExampleSectionReader_ReadAt() {
|
func ExampleSectionReader_ReadAt() {
|
||||||
r := strings.NewReader("some io.Reader stream to be read\n")
|
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)
|
buf := make([]byte, 6)
|
||||||
if _, err := s.ReadAt(buf, 10); err != nil {
|
if _, err := s.ReadAt(buf, 10); err != nil {
|
||||||
@ -186,30 +176,24 @@ func ExampleSectionReader_ReadAt() {
|
|||||||
|
|
||||||
func ExampleSectionReader_Seek() {
|
func ExampleSectionReader_Seek() {
|
||||||
r := strings.NewReader("some io.Reader stream to be read\n")
|
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 {
|
if _, err := s.Seek(10, io.SeekStart); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, 6)
|
if _, err := io.Copy(os.Stdout, s); err != nil {
|
||||||
if _, err := s.Read(buf); err != nil {
|
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%s\n", buf)
|
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// stream
|
// stream
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleSeeker_Seek() {
|
func ExampleSeeker_Seek() {
|
||||||
r := strings.NewReader("some io.Reader stream to be read\n")
|
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 {
|
if _, err := io.Copy(os.Stdout, r); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -220,8 +204,7 @@ func ExampleSeeker_Seek() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// some io.Reader stream to be read
|
// io.Reader stream to be read
|
||||||
// stream to be read
|
|
||||||
// read
|
// read
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,14 +230,14 @@ func ExamplePipe() {
|
|||||||
r, w := io.Pipe()
|
r, w := io.Pipe()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
fmt.Fprint(w, "some text to be read\n")
|
fmt.Fprint(w, "some io.Reader stream to be read\n")
|
||||||
w.Close()
|
w.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
if _, err := io.Copy(os.Stdout, r); err != nil {
|
||||||
buf.ReadFrom(r)
|
log.Fatal(err)
|
||||||
fmt.Print(buf.String())
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// some text to be read
|
// some io.Reader stream to be read
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user