aboutsummaryrefslogtreecommitdiff
path: root/src/bufio/example_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/bufio/example_test.go')
-rw-r--r--src/bufio/example_test.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/bufio/example_test.go b/src/bufio/example_test.go
index a864d11012..6d219aecc6 100644
--- a/src/bufio/example_test.go
+++ b/src/bufio/example_test.go
@@ -6,6 +6,7 @@ package bufio_test
import (
"bufio"
+ "bytes"
"fmt"
"os"
"strconv"
@@ -137,3 +138,36 @@ func ExampleScanner_emptyFinalToken() {
}
// Output: "1" "2" "3" "4" ""
}
+
+// Use a Scanner with a custom split function to parse a comma-separated
+// list with an empty final value but stops at the token "STOP".
+func ExampleScanner_earlyStop() {
+ onComma := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
+ i := bytes.IndexByte(data, ',')
+ if i == -1 {
+ if !atEOF {
+ return 0, nil, nil
+ }
+ // If we have reached the end, return the last token.
+ return 0, data, bufio.ErrFinalToken
+ }
+ // If the token is "STOP", stop the scanning and ignore the rest.
+ if string(data[:i]) == "STOP" {
+ return i + 1, nil, bufio.ErrFinalToken
+ }
+ // Otherwise, return the token before the comma.
+ return i + 1, data[:i], nil
+ }
+ const input = "1,2,STOP,4,"
+ scanner := bufio.NewScanner(strings.NewReader(input))
+ scanner.Split(onComma)
+ for scanner.Scan() {
+ fmt.Printf("Got a token %q\n", scanner.Text())
+ }
+ if err := scanner.Err(); err != nil {
+ fmt.Fprintln(os.Stderr, "reading input:", err)
+ }
+ // Output:
+ // Got a token "1"
+ // Got a token "2"
+}