| 1 |
// Copyright 2013 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. |
| 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() // Don't forget to flush! |
| 21 |
// Output: Hello, world! |
| 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 |
// Output: 1 2 3 4 |
| 34 |
} |
| 35 |
|
| 36 |
// ExampleWriter_ReadFrom demonstrates how to use the ReadFrom method of Writer. |
| 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 |
// Output: |
| 58 |
// Bytes written: 41 |
| 59 |
// Buffer contents: Hello, world! |
| 60 |
// This is a ReadFrom example. |
| 61 |
} |
| 62 |
|
| 63 |
// The simplest use of a Scanner, to read standard input as a set of lines. |
| 64 |
func ExampleScanner_lines() { |
| 65 |
scanner := bufio.NewScanner(os.Stdin) |
| 66 |
for scanner.Scan() { |
| 67 |
fmt.Println(scanner.Text()) // Println will add back the final '\n' |
| 68 |
} |
| 69 |
if err := scanner.Err(); err != nil { |
| 70 |
fmt.Fprintln(os.Stderr, "reading standard input:", err) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// Return the most recent call to Scan as a []byte. |
| 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 |
// Output: |
| 84 |
// true |
| 85 |
} |
| 86 |
|
| 87 |
// Use a Scanner to implement a simple word-count utility by scanning the |
| 88 |
// input as a sequence of space-delimited tokens. |
| 89 |
func ExampleScanner_words() { |
| 90 |
// An artificial input source. |
| 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 |
// Set the split function for the scanning operation. |
| 94 |
scanner.Split(bufio.ScanWords) |
| 95 |
// Count the words. |
| 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 |
// Output: 15 |
| 105 |
} |
| 106 |
|
| 107 |
// Use a Scanner with a custom split function (built by wrapping ScanWords) to validate |
| 108 |
// 32-bit decimal input. |
| 109 |
func ExampleScanner_custom() { |
| 110 |
// An artificial input source. |
| 111 |
const input = "1234 5678 1234567901234567890" |
| 112 |
scanner := bufio.NewScanner(strings.NewReader(input)) |
| 113 |
// Create a custom split function by wrapping the existing ScanWords function. |
| 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 |
// Set the split function for the scanning operation. |
| 122 |
scanner.Split(split) |
| 123 |
// Validate the input |
| 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 |
// Output: |
| 132 |
// 1234 |
| 133 |
// 5678 |
| 134 |
// Invalid input: strconv.ParseInt: parsing "1234567901234567890": value out of range |
| 135 |
} |
| 136 |
|
| 137 |
// Use a Scanner with a custom split function to parse a comma-separated |
| 138 |
// list with an empty final value. |
| 139 |
func ExampleScanner_emptyFinalToken() { |
| 140 |
// Comma-separated list; last entry is empty. |
| 141 |
const input = "1,2,3,4," |
| 142 |
scanner := bufio.NewScanner(strings.NewReader(input)) |
| 143 |
// Define a split function that separates on commas. |
| 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 |
// There is one final token to be delivered, which may be the empty string. |
| 154 |
// Returning bufio.ErrFinalToken here tells Scan there are no more tokens after this |
| 155 |
// but does not trigger an error to be returned from Scan itself. |
| 156 |
return 0, data, bufio.ErrFinalToken |
| 157 |
} |
| 158 |
scanner.Split(onComma) |
| 159 |
// Scan. |
| 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 |
// Output: "1" "2" "3" "4" "" |
| 167 |
} |
| 168 |
|
| 169 |
// Use a Scanner with a custom split function to parse a comma-separated |
| 170 |
// list with an empty final value but stops at the token "STOP". |
| 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 |
// If we have reached the end, return the last token. |
| 179 |
return 0, data, bufio.ErrFinalToken |
| 180 |
} |
| 181 |
// If the token is "STOP", stop the scanning and ignore the rest. |
| 182 |
if string(data[:i]) == "STOP" { |
| 183 |
return i + 1, nil, bufio.ErrFinalToken |
| 184 |
} |
| 185 |
// Otherwise, return the token before the comma. |
| 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 |
// Output: |
| 198 |
// Got a token "1" |
| 199 |
// Got a token "2" |
| 200 |
} |