Source file
src/io/example_test.go
Documentation: io
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 io.ReadAll(r)
146
147
148
149 }
150
151 func ExampleSectionReader() {
152 r := strings.NewReader("some io.Reader stream to be read\n")
153 s := io.NewSectionReader(r, 5, 17)
154
155 if _, err := io.Copy(os.Stdout, s); err != nil {
156 log.Fatal(err)
157 }
158
159
160
161 }
162
163 func ExampleSectionReader_ReadAt() {
164 r := strings.NewReader("some io.Reader stream to be read\n")
165 s := io.NewSectionReader(r, 5, 17)
166
167 buf := make([]byte, 6)
168 if _, err := s.ReadAt(buf, 10); err != nil {
169 log.Fatal(err)
170 }
171
172 fmt.Printf("%s\n", buf)
173
174
175
176 }
177
178 func ExampleSectionReader_Seek() {
179 r := strings.NewReader("some io.Reader stream to be read\n")
180 s := io.NewSectionReader(r, 5, 17)
181
182 if _, err := s.Seek(10, io.SeekStart); err != nil {
183 log.Fatal(err)
184 }
185
186 if _, err := io.Copy(os.Stdout, s); err != nil {
187 log.Fatal(err)
188 }
189
190
191
192 }
193
194 func ExampleSeeker_Seek() {
195 r := strings.NewReader("some io.Reader stream to be read\n")
196
197 r.Seek(5, io.SeekStart)
198 if _, err := io.Copy(os.Stdout, r); err != nil {
199 log.Fatal(err)
200 }
201
202 r.Seek(-5, io.SeekEnd)
203 if _, err := io.Copy(os.Stdout, r); err != nil {
204 log.Fatal(err)
205 }
206
207
208
209
210 }
211
212 func ExampleMultiWriter() {
213 r := strings.NewReader("some io.Reader stream to be read\n")
214
215 var buf1, buf2 bytes.Buffer
216 w := io.MultiWriter(&buf1, &buf2)
217
218 if _, err := io.Copy(w, r); err != nil {
219 log.Fatal(err)
220 }
221
222 fmt.Print(buf1.String())
223 fmt.Print(buf2.String())
224
225
226
227
228 }
229
230 func ExamplePipe() {
231 r, w := io.Pipe()
232
233 go func() {
234 fmt.Fprint(w, "some io.Reader stream to be read\n")
235 w.Close()
236 }()
237
238 if _, err := io.Copy(os.Stdout, r); err != nil {
239 log.Fatal(err)
240 }
241
242
243
244 }
245
246 func ExampleReadAll() {
247 r := strings.NewReader("Go is a general-purpose language designed with systems programming in mind.")
248
249 b, err := io.ReadAll(r)
250 if err != nil {
251 log.Fatal(err)
252 }
253
254 fmt.Printf("%s", b)
255
256
257
258 }
259
View as plain text