Source file
src/bufio/example_test.go
Documentation: bufio
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 ExampleScanner_lines() {
38 scanner := bufio.NewScanner(os.Stdin)
39 for scanner.Scan() {
40 fmt.Println(scanner.Text())
41 }
42 if err := scanner.Err(); err != nil {
43 fmt.Fprintln(os.Stderr, "reading standard input:", err)
44 }
45 }
46
47
48 func ExampleScanner_Bytes() {
49 scanner := bufio.NewScanner(strings.NewReader("gopher"))
50 for scanner.Scan() {
51 fmt.Println(len(scanner.Bytes()) == 6)
52 }
53 if err := scanner.Err(); err != nil {
54 fmt.Fprintln(os.Stderr, "shouldn't see an error scanning a string")
55 }
56
57
58 }
59
60
61
62 func ExampleScanner_words() {
63
64 const input = "Now is the winter of our discontent,\nMade glorious summer by this sun of York.\n"
65 scanner := bufio.NewScanner(strings.NewReader(input))
66
67 scanner.Split(bufio.ScanWords)
68
69 count := 0
70 for scanner.Scan() {
71 count++
72 }
73 if err := scanner.Err(); err != nil {
74 fmt.Fprintln(os.Stderr, "reading input:", err)
75 }
76 fmt.Printf("%d\n", count)
77
78 }
79
80
81
82 func ExampleScanner_custom() {
83
84 const input = "1234 5678 1234567901234567890"
85 scanner := bufio.NewScanner(strings.NewReader(input))
86
87 split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
88 advance, token, err = bufio.ScanWords(data, atEOF)
89 if err == nil && token != nil {
90 _, err = strconv.ParseInt(string(token), 10, 32)
91 }
92 return
93 }
94
95 scanner.Split(split)
96
97 for scanner.Scan() {
98 fmt.Printf("%s\n", scanner.Text())
99 }
100
101 if err := scanner.Err(); err != nil {
102 fmt.Printf("Invalid input: %s", err)
103 }
104
105
106
107
108 }
109
110
111
112 func ExampleScanner_emptyFinalToken() {
113
114 const input = "1,2,3,4,"
115 scanner := bufio.NewScanner(strings.NewReader(input))
116
117 onComma := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
118 for i := 0; i < len(data); i++ {
119 if data[i] == ',' {
120 return i + 1, data[:i], nil
121 }
122 }
123 if !atEOF {
124 return 0, nil, nil
125 }
126
127
128
129 return 0, data, bufio.ErrFinalToken
130 }
131 scanner.Split(onComma)
132
133 for scanner.Scan() {
134 fmt.Printf("%q ", scanner.Text())
135 }
136 if err := scanner.Err(); err != nil {
137 fmt.Fprintln(os.Stderr, "reading input:", err)
138 }
139
140 }
141
142
143
144 func ExampleScanner_earlyStop() {
145 onComma := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
146 i := bytes.IndexByte(data, ',')
147 if i == -1 {
148 if !atEOF {
149 return 0, nil, nil
150 }
151
152 return 0, data, bufio.ErrFinalToken
153 }
154
155 if string(data[:i]) == "STOP" {
156 return i + 1, nil, bufio.ErrFinalToken
157 }
158
159 return i + 1, data[:i], nil
160 }
161 const input = "1,2,STOP,4,"
162 scanner := bufio.NewScanner(strings.NewReader(input))
163 scanner.Split(onComma)
164 for scanner.Scan() {
165 fmt.Printf("Got a token %q\n", scanner.Text())
166 }
167 if err := scanner.Err(); err != nil {
168 fmt.Fprintln(os.Stderr, "reading input:", err)
169 }
170
171
172
173 }
174
View as plain text