Source file src/bufio/example_test.go

     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  // The simplest use of a Scanner, to read standard input as a set of lines.
    37  func ExampleScanner_lines() {
    38  	scanner := bufio.NewScanner(os.Stdin)
    39  	for scanner.Scan() {
    40  		fmt.Println(scanner.Text()) // Println will add back the final '\n'
    41  	}
    42  	if err := scanner.Err(); err != nil {
    43  		fmt.Fprintln(os.Stderr, "reading standard input:", err)
    44  	}
    45  }
    46  
    47  // Return the most recent call to Scan as a []byte.
    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  	// Output:
    57  	// true
    58  }
    59  
    60  // Use a Scanner to implement a simple word-count utility by scanning the
    61  // input as a sequence of space-delimited tokens.
    62  func ExampleScanner_words() {
    63  	// An artificial input source.
    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  	// Set the split function for the scanning operation.
    67  	scanner.Split(bufio.ScanWords)
    68  	// Count the words.
    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  	// Output: 15
    78  }
    79  
    80  // Use a Scanner with a custom split function (built by wrapping ScanWords) to validate
    81  // 32-bit decimal input.
    82  func ExampleScanner_custom() {
    83  	// An artificial input source.
    84  	const input = "1234 5678 1234567901234567890"
    85  	scanner := bufio.NewScanner(strings.NewReader(input))
    86  	// Create a custom split function by wrapping the existing ScanWords function.
    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  	// Set the split function for the scanning operation.
    95  	scanner.Split(split)
    96  	// Validate the input
    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  	// Output:
   105  	// 1234
   106  	// 5678
   107  	// Invalid input: strconv.ParseInt: parsing "1234567901234567890": value out of range
   108  }
   109  
   110  // Use a Scanner with a custom split function to parse a comma-separated
   111  // list with an empty final value.
   112  func ExampleScanner_emptyFinalToken() {
   113  	// Comma-separated list; last entry is empty.
   114  	const input = "1,2,3,4,"
   115  	scanner := bufio.NewScanner(strings.NewReader(input))
   116  	// Define a split function that separates on commas.
   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  		// There is one final token to be delivered, which may be the empty string.
   127  		// Returning bufio.ErrFinalToken here tells Scan there are no more tokens after this
   128  		// but does not trigger an error to be returned from Scan itself.
   129  		return 0, data, bufio.ErrFinalToken
   130  	}
   131  	scanner.Split(onComma)
   132  	// Scan.
   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  	// Output: "1" "2" "3" "4" ""
   140  }
   141  
   142  // Use a Scanner with a custom split function to parse a comma-separated
   143  // list with an empty final value but stops at the token "STOP".
   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  			// If we have reached the end, return the last token.
   152  			return 0, data, bufio.ErrFinalToken
   153  		}
   154  		// If the token is "STOP", stop the scanning and ignore the rest.
   155  		if string(data[:i]) == "STOP" {
   156  			return i + 1, nil, bufio.ErrFinalToken
   157  		}
   158  		// Otherwise, return the token before the comma.
   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  	// Output:
   171  	// Got a token "1"
   172  	// Got a token "2"
   173  }
   174  

View as plain text