Source file
src/bufio/example_test.go
1
2
3
4
5 package bufio_test
6
7 import (
8 "bufio"
9 "bytes"
10 "fmt"
11 "os"
12 "strconv"
13 "strings"
14 )
15
16 func ExampleWriter() {
17 w := bufio.NewWriter(os.Stdout)
18 fmt.Fprint(w, "Hello, ")
19 fmt.Fprint(w, "world!")
20 w.Flush()
21
22 }
23
24 func ExampleWriter_AvailableBuffer() {
25 w := bufio.NewWriter(os.Stdout)
26 for _, i := range []int64{1, 2, 3, 4} {
27 b := w.AvailableBuffer()
28 b = strconv.AppendInt(b, i, 10)
29 b = append(b, ' ')
30 w.Write(b)
31 }
32 w.Flush()
33
34 }
35
36
37 func ExampleWriter_ReadFrom() {
38 var buf bytes.Buffer
39 writer := bufio.NewWriter(&buf)
40
41 data := "Hello, world!\nThis is a ReadFrom example."
42 reader := strings.NewReader(data)
43
44 n, err := writer.ReadFrom(reader)
45 if err != nil {
46 fmt.Println("ReadFrom Error:", err)
47 return
48 }
49
50 if err = writer.Flush(); err != nil {
51 fmt.Println("Flush Error:", err)
52 return
53 }
54
55 fmt.Println("Bytes written:", n)
56 fmt.Println("Buffer contents:", buf.String())
57
58
59
60
61 }
62
63
64 func ExampleScanner_lines() {
65 scanner := bufio.NewScanner(os.Stdin)
66 for scanner.Scan() {
67 fmt.Println(scanner.Text())
68 }
69 if err := scanner.Err(); err != nil {
70 fmt.Fprintln(os.Stderr, "reading standard input:", err)
71 }
72 }
73
74
75 func ExampleScanner_Bytes() {
76 scanner := bufio.NewScanner(strings.NewReader("gopher"))
77 for scanner.Scan() {
78 fmt.Println(len(scanner.Bytes()) == 6)
79 }
80 if err := scanner.Err(); err != nil {
81 fmt.Fprintln(os.Stderr, "shouldn't see an error scanning a string")
82 }
83
84
85 }
86
87
88
89 func ExampleScanner_words() {
90
91 const input = "Now is the winter of our discontent,\nMade glorious summer by this sun of York.\n"
92 scanner := bufio.NewScanner(strings.NewReader(input))
93
94 scanner.Split(bufio.ScanWords)
95
96 count := 0
97 for scanner.Scan() {
98 count++
99 }
100 if err := scanner.Err(); err != nil {
101 fmt.Fprintln(os.Stderr, "reading input:", err)
102 }
103 fmt.Printf("%d\n", count)
104
105 }
106
107
108
109 func ExampleScanner_custom() {
110
111 const input = "1234 5678 1234567901234567890"
112 scanner := bufio.NewScanner(strings.NewReader(input))
113
114 split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
115 advance, token, err = bufio.ScanWords(data, atEOF)
116 if err == nil && token != nil {
117 _, err = strconv.ParseInt(string(token), 10, 32)
118 }
119 return
120 }
121
122 scanner.Split(split)
123
124 for scanner.Scan() {
125 fmt.Printf("%s\n", scanner.Text())
126 }
127
128 if err := scanner.Err(); err != nil {
129 fmt.Printf("Invalid input: %s", err)
130 }
131
132
133
134
135 }
136
137
138
139 func ExampleScanner_emptyFinalToken() {
140
141 const input = "1,2,3,4,"
142 scanner := bufio.NewScanner(strings.NewReader(input))
143
144 onComma := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
145 for i := 0; i < len(data); i++ {
146 if data[i] == ',' {
147 return i + 1, data[:i], nil
148 }
149 }
150 if !atEOF {
151 return 0, nil, nil
152 }
153
154
155
156 return 0, data, bufio.ErrFinalToken
157 }
158 scanner.Split(onComma)
159
160 for scanner.Scan() {
161 fmt.Printf("%q ", scanner.Text())
162 }
163 if err := scanner.Err(); err != nil {
164 fmt.Fprintln(os.Stderr, "reading input:", err)
165 }
166
167 }
168
169
170
171 func ExampleScanner_earlyStop() {
172 onComma := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
173 i := bytes.IndexByte(data, ',')
174 if i == -1 {
175 if !atEOF {
176 return 0, nil, nil
177 }
178
179 return 0, data, bufio.ErrFinalToken
180 }
181
182 if string(data[:i]) == "STOP" {
183 return i + 1, nil, bufio.ErrFinalToken
184 }
185
186 return i + 1, data[:i], nil
187 }
188 const input = "1,2,STOP,4,"
189 scanner := bufio.NewScanner(strings.NewReader(input))
190 scanner.Split(onComma)
191 for scanner.Scan() {
192 fmt.Printf("Got a token %q\n", scanner.Text())
193 }
194 if err := scanner.Err(); err != nil {
195 fmt.Fprintln(os.Stderr, "reading input:", err)
196 }
197
198
199
200 }
201
View as plain text