1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 r
:= strings
.NewReader("some io.Reader stream to be read\n")
20 if _
, err
:= io
.Copy(os
.Stdout
, r
); err
!= nil {
25 // some io.Reader stream to be read
28 func ExampleCopyBuffer() {
29 r1
:= strings
.NewReader("first reader\n")
30 r2
:= strings
.NewReader("second reader\n")
31 buf
:= make([]byte, 8)
33 // buf is used here...
34 if _
, err
:= io
.CopyBuffer(os
.Stdout
, r1
, buf
); err
!= nil {
38 // ... reused here also. No need to allocate an extra buffer.
39 if _
, err
:= io
.CopyBuffer(os
.Stdout
, r2
, buf
); err
!= nil {
49 r
:= strings
.NewReader("some io.Reader stream to be read")
51 if _
, err
:= io
.CopyN(os
.Stdout
, r
, 5); err
!= nil {
59 func ExampleReadAtLeast() {
60 r
:= strings
.NewReader("some io.Reader stream to be read\n")
62 buf
:= make([]byte, 33)
63 if _
, err
:= io
.ReadAtLeast(r
, buf
, 4); err
!= nil {
66 fmt
.Printf("%s\n", buf
)
68 // buffer smaller than minimal read size.
69 shortBuf
:= make([]byte, 3)
70 if _
, err
:= io
.ReadAtLeast(r
, shortBuf
, 4); err
!= nil {
71 fmt
.Println("error:", err
)
74 // minimal read size bigger than io.Reader stream
75 longBuf
:= make([]byte, 64)
76 if _
, err
:= io
.ReadAtLeast(r
, longBuf
, 64); err
!= nil {
77 fmt
.Println("error:", err
)
81 // some io.Reader stream to be read
83 // error: short buffer
87 func ExampleReadFull() {
88 r
:= strings
.NewReader("some io.Reader stream to be read\n")
90 buf
:= make([]byte, 4)
91 if _
, err
:= io
.ReadFull(r
, buf
); err
!= nil {
94 fmt
.Printf("%s\n", buf
)
96 // minimal read size bigger than io.Reader stream
97 longBuf
:= make([]byte, 64)
98 if _
, err
:= io
.ReadFull(r
, longBuf
); err
!= nil {
99 fmt
.Println("error:", err
)
104 // error: unexpected EOF
107 func ExampleWriteString() {
108 io
.WriteString(os
.Stdout
, "Hello World")
110 // Output: Hello World
113 func ExampleLimitReader() {
114 r
:= strings
.NewReader("some io.Reader stream to be read\n")
115 lr
:= io
.LimitReader(r
, 4)
117 if _
, err
:= io
.Copy(os
.Stdout
, lr
); err
!= nil {
125 func ExampleMultiReader() {
126 r1
:= strings
.NewReader("first reader ")
127 r2
:= strings
.NewReader("second reader ")
128 r3
:= strings
.NewReader("third reader\n")
129 r
:= io
.MultiReader(r1
, r2
, r3
)
131 if _
, err
:= io
.Copy(os
.Stdout
, r
); err
!= nil {
136 // first reader second reader third reader
139 func ExampleTeeReader() {
140 r
:= strings
.NewReader("some io.Reader stream to be read\n")
142 tee
:= io
.TeeReader(r
, &buf
)
144 printall
:= func(r io
.Reader
) {
145 b
, err
:= ioutil
.ReadAll(r
)
157 // some io.Reader stream to be read
158 // some io.Reader stream to be read
161 func ExampleSectionReader() {
162 r
:= strings
.NewReader("some io.Reader stream to be read\n")
163 s
:= io
.NewSectionReader(r
, 5, 17)
165 if _
, err
:= io
.Copy(os
.Stdout
, s
); err
!= nil {
173 func ExampleSectionReader_ReadAt() {
174 r
:= strings
.NewReader("some io.Reader stream to be read\n")
175 s
:= io
.NewSectionReader(r
, 5, 16)
177 buf
:= make([]byte, 6)
178 if _
, err
:= s
.ReadAt(buf
, 10); err
!= nil {
182 fmt
.Printf("%s\n", buf
)
188 func ExampleSectionReader_Seek() {
189 r
:= strings
.NewReader("some io.Reader stream to be read\n")
190 s
:= io
.NewSectionReader(r
, 5, 16)
192 if _
, err
:= s
.Seek(10, 0); err
!= nil {
196 buf
:= make([]byte, 6)
197 if _
, err
:= s
.Read(buf
); err
!= nil {
201 fmt
.Printf("%s\n", buf
)
207 func ExampleMultiWriter() {
208 r
:= strings
.NewReader("some io.Reader stream to be read\n")
210 var buf1
, buf2 bytes
.Buffer
211 w
:= io
.MultiWriter(&buf1
, &buf2
)
213 if _
, err
:= io
.Copy(w
, r
); err
!= nil {
217 fmt
.Print(buf1
.String())
218 fmt
.Print(buf2
.String())
221 // some io.Reader stream to be read
222 // some io.Reader stream to be read