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