Source file
src/io/example_test.go
1
2
3
4
5 package io_test
6
7 import (
8 "bytes"
9 "fmt"
10 "io"
11 "log"
12 "os"
13 "strings"
14 )
15
16 func ExampleCopy() {
17 r := strings.NewReader("some io.Reader stream to be read\n")
18
19 if _, err := io.Copy(os.Stdout, r); err != nil {
20 log.Fatal(err)
21 }
22
23
24
25 }
26
27 func ExampleCopyBuffer() {
28 r1 := strings.NewReader("first reader\n")
29 r2 := strings.NewReader("second reader\n")
30 buf := make([]byte, 8)
31
32
33 if _, err := io.CopyBuffer(os.Stdout, r1, buf); err != nil {
34 log.Fatal(err)
35 }
36
37
38 if _, err := io.CopyBuffer(os.Stdout, r2, buf); err != nil {
39 log.Fatal(err)
40 }
41
42
43
44
45 }
46
47 func ExampleCopyN() {
48 r := strings.NewReader("some io.Reader stream to be read")
49
50 if _, err := io.CopyN(os.Stdout, r, 4); err != nil {
51 log.Fatal(err)
52 }
53
54
55
56 }
57
58 func ExampleReadAtLeast() {
59 r := strings.NewReader("some io.Reader stream to be read\n")
60
61 buf := make([]byte, 14)
62 if _, err := io.ReadAtLeast(r, buf, 4); err != nil {
63 log.Fatal(err)
64 }
65 fmt.Printf("%s\n", buf)
66
67
68 shortBuf := make([]byte, 3)
69 if _, err := io.ReadAtLeast(r, shortBuf, 4); err != nil {
70 fmt.Println("error:", err)
71 }
72
73
74 longBuf := make([]byte, 64)
75 if _, err := io.ReadAtLeast(r, longBuf, 64); err != nil {
76 fmt.Println("error:", err)
77 }
78
79
80
81
82
83 }
84
85 func ExampleReadFull() {
86 r := strings.NewReader("some io.Reader stream to be read\n")
87
88 buf := make([]byte, 4)
89 if _, err := io.ReadFull(r, buf); err != nil {
90 log.Fatal(err)
91 }
92 fmt.Printf("%s\n", buf)
93
94
95 longBuf := make([]byte, 64)
96 if _, err := io.ReadFull(r, longBuf); err != nil {
97 fmt.Println("error:", err)
98 }
99
100
101
102
103 }
104
105 func ExampleWriteString() {
106 if _, err := io.WriteString(os.Stdout, "Hello World"); err != nil {
107 log.Fatal(err)
108 }
109
110
111 }
112
113 func ExampleLimitReader() {
114 r := strings.NewReader("some io.Reader stream to be read\n")
115 lr := io.LimitReader(r, 4)
116
117 if _, err := io.Copy(os.Stdout, lr); err != nil {
118 log.Fatal(err)
119 }
120
121
122
123 }
124
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)
130
131 if _, err := io.Copy(os.Stdout, r); err != nil {
132 log.Fatal(err)
133 }
134
135
136
137 }
138
139 func ExampleTeeReader() {
140 var r io.Reader = strings.NewReader("some io.Reader stream to be read\n")
141
142 r = io.TeeReader(r, os.Stdout)
143
144
145 if _, err := io.ReadAll(r); err != nil {
146 log.Fatal(err)
147 }
148
149
150
151 }
152
153 func ExampleSectionReader() {
154 r := strings.NewReader("some io.Reader stream to be read\n")
155 s := io.NewSectionReader(r, 5, 17)
156
157 if _, err := io.Copy(os.Stdout, s); err != nil {
158 log.Fatal(err)
159 }
160
161
162
163 }
164
165 func ExampleSectionReader_Read() {
166 r := strings.NewReader("some io.Reader stream to be read\n")
167 s := io.NewSectionReader(r, 5, 17)
168
169 buf := make([]byte, 9)
170 if _, err := s.Read(buf); err != nil {
171 log.Fatal(err)
172 }
173
174 fmt.Printf("%s\n", buf)
175
176
177
178 }
179
180 func ExampleSectionReader_ReadAt() {
181 r := strings.NewReader("some io.Reader stream to be read\n")
182 s := io.NewSectionReader(r, 5, 17)
183
184 buf := make([]byte, 6)
185 if _, err := s.ReadAt(buf, 10); err != nil {
186 log.Fatal(err)
187 }
188
189 fmt.Printf("%s\n", buf)
190
191
192
193 }
194
195 func ExampleSectionReader_Seek() {
196 r := strings.NewReader("some io.Reader stream to be read\n")
197 s := io.NewSectionReader(r, 5, 17)
198
199 if _, err := s.Seek(10, io.SeekStart); err != nil {
200 log.Fatal(err)
201 }
202
203 if _, err := io.Copy(os.Stdout, s); err != nil {
204 log.Fatal(err)
205 }
206
207
208
209 }
210
211 func ExampleSectionReader_Size() {
212 r := strings.NewReader("some io.Reader stream to be read\n")
213 s := io.NewSectionReader(r, 5, 17)
214
215 fmt.Println(s.Size())
216
217
218
219 }
220
221 func ExampleSeeker_Seek() {
222 r := strings.NewReader("some io.Reader stream to be read\n")
223
224 r.Seek(5, io.SeekStart)
225 if _, err := io.Copy(os.Stdout, r); err != nil {
226 log.Fatal(err)
227 }
228
229 r.Seek(-5, io.SeekEnd)
230 if _, err := io.Copy(os.Stdout, r); err != nil {
231 log.Fatal(err)
232 }
233
234
235
236
237 }
238
239 func ExampleMultiWriter() {
240 r := strings.NewReader("some io.Reader stream to be read\n")
241
242 var buf1, buf2 bytes.Buffer
243 w := io.MultiWriter(&buf1, &buf2)
244
245 if _, err := io.Copy(w, r); err != nil {
246 log.Fatal(err)
247 }
248
249 fmt.Print(buf1.String())
250 fmt.Print(buf2.String())
251
252
253
254
255 }
256
257 func ExamplePipe() {
258 r, w := io.Pipe()
259
260 go func() {
261 fmt.Fprint(w, "some io.Reader stream to be read\n")
262 w.Close()
263 }()
264
265 if _, err := io.Copy(os.Stdout, r); err != nil {
266 log.Fatal(err)
267 }
268
269
270
271 }
272
273 func ExampleReadAll() {
274 r := strings.NewReader("Go is a general-purpose language designed with systems programming in mind.")
275
276 b, err := io.ReadAll(r)
277 if err != nil {
278 log.Fatal(err)
279 }
280
281 fmt.Printf("%s", b)
282
283
284
285 }
286
View as plain text