]> Cypherpunks repositories - gostls13.git/commit
bufio: new Scanner interface
authorRob Pike <r@golang.org>
Wed, 20 Feb 2013 20:14:31 +0000 (12:14 -0800)
committerRob Pike <r@golang.org>
Wed, 20 Feb 2013 20:14:31 +0000 (12:14 -0800)
commit55ad7b9bfe86c90cff55e0e8926fd8ff6b3b5182
treee2c5303f4f4f7c47cb498db4d9d07899303768a9
parent75e7308be8dc13e53b4f39aad67f286e79ac5313
bufio: new Scanner interface

Add a new, simple interface for scanning (probably textual) data,
based on a new type called Scanner. It does its own internal buffering,
so should be plausibly efficient even without injecting a bufio.Reader.
The format of the input is defined by a "split function", by default
splitting into lines. Other implemented split functions include single
bytes, single runes, and space-separated words.

Here's the loop to scan stdin as a file of lines:

        s := bufio.NewScanner(os.Stdin)
        for s.Scan() {
                fmt.Printf("%s\n", s.Bytes())
        }
        if s.Err() != nil {
                log.Fatal(s.Err())
        }

While we're dealing with spaces, define what space means to strings.Fields.

Fixes #4802.

R=adg, rogpeppe, bradfitz, rsc
CC=golang-dev
https://golang.org/cl/7322088
src/pkg/bufio/bufio_test.go
src/pkg/bufio/export_test.go [new file with mode: 0644]
src/pkg/bufio/scan.go [new file with mode: 0644]
src/pkg/bufio/scan_test.go [new file with mode: 0644]
src/pkg/strings/strings.go