...

Source file src/bufio/example_test.go

Documentation: bufio

		 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  	"fmt"
		10  	"os"
		11  	"strconv"
		12  	"strings"
		13  )
		14  
		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!
		21  }
		22  
		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'
		28  	}
		29  	if err := scanner.Err(); err != nil {
		30  		fmt.Fprintln(os.Stderr, "reading standard input:", err)
		31  	}
		32  }
		33  
		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)
		39  	}
		40  	if err := scanner.Err(); err != nil {
		41  		fmt.Fprintln(os.Stderr, "shouldn't see an error scanning a string")
		42  	}
		43  	// Output:
		44  	// true
		45  }
		46  
		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++
		59  	}
		60  	if err := scanner.Err(); err != nil {
		61  		fmt.Fprintln(os.Stderr, "reading input:", err)
		62  	}
		63  	fmt.Printf("%d\n", count)
		64  	// Output: 15
		65  }
		66  
		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)
		78  		}
		79  		return
		80  	}
		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())
		86  	}
		87  
		88  	if err := scanner.Err(); err != nil {
		89  		fmt.Printf("Invalid input: %s", err)
		90  	}
		91  	// Output:
		92  	// 1234
		93  	// 5678
		94  	// Invalid input: strconv.ParseInt: parsing "1234567901234567890": value out of range
		95  }
		96  
		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
	 108  			}
	 109  		}
	 110  		if !atEOF {
	 111  			return 0, nil, nil
	 112  		}
	 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
	 117  	}
	 118  	scanner.Split(onComma)
	 119  	// Scan.
	 120  	for scanner.Scan() {
	 121  		fmt.Printf("%q ", scanner.Text())
	 122  	}
	 123  	if err := scanner.Err(); err != nil {
	 124  		fmt.Fprintln(os.Stderr, "reading input:", err)
	 125  	}
	 126  	// Output: "1" "2" "3" "4" ""
	 127  }
	 128  

View as plain text