Fix "PR c++/92804 ICE trying to use concept as a nested-name-specifier"
[official-gcc.git] / libgo / go / bufio / example_test.go
blob8885d40549f1532010a320d00964f19bffbb5846
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.
5 package bufio_test
7 import (
8 "bufio"
9 "fmt"
10 "os"
11 "strconv"
12 "strings"
15 func ExampleWriter() {
16 w := bufio.NewWriter(os.Stdout)
17 fmt.Fprint(w, "Hello, ")
18 fmt.Fprint(w, "world!")
19 w.Flush() // Don't forget to flush!
20 // Output: Hello, world!
23 // The simplest use of a Scanner, to read standard input as a set of lines.
24 func ExampleScanner_lines() {
25 scanner := bufio.NewScanner(os.Stdin)
26 for scanner.Scan() {
27 fmt.Println(scanner.Text()) // Println will add back the final '\n'
29 if err := scanner.Err(); err != nil {
30 fmt.Fprintln(os.Stderr, "reading standard input:", err)
34 // Return the most recent call to Scan as a []byte.
35 func ExampleScanner_Bytes() {
36 scanner := bufio.NewScanner(strings.NewReader("gopher"))
37 for scanner.Scan() {
38 fmt.Println(len(scanner.Bytes()) == 6)
40 if err := scanner.Err(); err != nil {
41 fmt.Fprintln(os.Stderr, "shouldn't see an error scanning a string")
43 // Output:
44 // true
47 // Use a Scanner to implement a simple word-count utility by scanning the
48 // input as a sequence of space-delimited tokens.
49 func ExampleScanner_words() {
50 // An artificial input source.
51 const input = "Now is the winter of our discontent,\nMade glorious summer by this sun of York.\n"
52 scanner := bufio.NewScanner(strings.NewReader(input))
53 // Set the split function for the scanning operation.
54 scanner.Split(bufio.ScanWords)
55 // Count the words.
56 count := 0
57 for scanner.Scan() {
58 count++
60 if err := scanner.Err(); err != nil {
61 fmt.Fprintln(os.Stderr, "reading input:", err)
63 fmt.Printf("%d\n", count)
64 // Output: 15
67 // Use a Scanner with a custom split function (built by wrapping ScanWords) to validate
68 // 32-bit decimal input.
69 func ExampleScanner_custom() {
70 // An artificial input source.
71 const input = "1234 5678 1234567901234567890"
72 scanner := bufio.NewScanner(strings.NewReader(input))
73 // Create a custom split function by wrapping the existing ScanWords function.
74 split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
75 advance, token, err = bufio.ScanWords(data, atEOF)
76 if err == nil && token != nil {
77 _, err = strconv.ParseInt(string(token), 10, 32)
79 return
81 // Set the split function for the scanning operation.
82 scanner.Split(split)
83 // Validate the input
84 for scanner.Scan() {
85 fmt.Printf("%s\n", scanner.Text())
88 if err := scanner.Err(); err != nil {
89 fmt.Printf("Invalid input: %s", err)
91 // Output:
92 // 1234
93 // 5678
94 // Invalid input: strconv.ParseInt: parsing "1234567901234567890": value out of range
97 // Use a Scanner with a custom split function to parse a comma-separated
98 // list with an empty final value.
99 func ExampleScanner_emptyFinalToken() {
100 // Comma-separated list; last entry is empty.
101 const input = "1,2,3,4,"
102 scanner := bufio.NewScanner(strings.NewReader(input))
103 // Define a split function that separates on commas.
104 onComma := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
105 for i := 0; i < len(data); i++ {
106 if data[i] == ',' {
107 return i + 1, data[:i], nil
110 if !atEOF {
111 return 0, nil, nil
113 // There is one final token to be delivered, which may be the empty string.
114 // Returning bufio.ErrFinalToken here tells Scan there are no more tokens after this
115 // but does not trigger an error to be returned from Scan itself.
116 return 0, data, bufio.ErrFinalToken
118 scanner.Split(onComma)
119 // Scan.
120 for scanner.Scan() {
121 fmt.Printf("%q ", scanner.Text())
123 if err := scanner.Err(); err != nil {
124 fmt.Fprintln(os.Stderr, "reading input:", err)
126 // Output: "1" "2" "3" "4" ""