* tree-ssa-loop-ivopts.c (estimated_stmt_executions_int): Use
[official-gcc.git] / libgo / go / io / example_test.go
blob412dfb3b921ba43e2b30e0adfb047cbce577e035
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.
5 package io_test
7 import (
8 "bytes"
9 "fmt"
10 "io"
11 "io/ioutil"
12 "log"
13 "os"
14 "strings"
17 func ExampleCopy() {
18 r := strings.NewReader("some io.Reader stream to be read\n")
20 if _, err := io.Copy(os.Stdout, r); err != nil {
21 log.Fatal(err)
24 // Output:
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 {
35 log.Fatal(err)
38 // ... reused here also. No need to allocate an extra buffer.
39 if _, err := io.CopyBuffer(os.Stdout, r2, buf); err != nil {
40 log.Fatal(err)
43 // Output:
44 // first reader
45 // second reader
48 func ExampleCopyN() {
49 r := strings.NewReader("some io.Reader stream to be read")
51 if _, err := io.CopyN(os.Stdout, r, 5); err != nil {
52 log.Fatal(err)
55 // Output:
56 // some
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 {
64 log.Fatal(err)
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)
80 // Output:
81 // some io.Reader stream to be read
83 // error: short buffer
84 // error: EOF
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 {
92 log.Fatal(err)
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)
102 // Output:
103 // some
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 {
118 log.Fatal(err)
121 // Output:
122 // some
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 {
132 log.Fatal(err)
135 // Output:
136 // first reader second reader third reader
139 func ExampleTeeReader() {
140 r := strings.NewReader("some io.Reader stream to be read\n")
141 var buf bytes.Buffer
142 tee := io.TeeReader(r, &buf)
144 printall := func(r io.Reader) {
145 b, err := ioutil.ReadAll(r)
146 if err != nil {
147 log.Fatal(err)
150 fmt.Printf("%s", b)
153 printall(tee)
154 printall(&buf)
156 // Output:
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 {
166 log.Fatal(err)
169 // Output:
170 // io.Reader stream
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 {
179 log.Fatal(err)
182 fmt.Printf("%s\n", buf)
184 // Output:
185 // stream
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 {
193 log.Fatal(err)
196 buf := make([]byte, 6)
197 if _, err := s.Read(buf); err != nil {
198 log.Fatal(err)
201 fmt.Printf("%s\n", buf)
203 // Output:
204 // stream
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 {
214 log.Fatal(err)
217 fmt.Print(buf1.String())
218 fmt.Print(buf2.String())
220 // Output:
221 // some io.Reader stream to be read
222 // some io.Reader stream to be read