golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.41k stars 17.7k forks source link

proposal: bufio: Scanner.IterText/Scanner.IterBytes #70657

Open pkierski opened 15 hours ago

pkierski commented 15 hours ago

Proposal Details

Once iterators are introduced in 1.23 there is growing number of libraries based on this feature. Reading data by lines or words are quite common task and could be accomplished with for ... range loop. It requires just two simple methods for bufio.Scanner:

func (s *Scanner) IterText() iter.Seq[string] {
    return func(yield func(string) bool) {
        for s.Scan() {
            if !yield(s.Text()) {
                break
            }
        }
    }
}

func (s *Scanner) IterBytes() iter.Seq[[]byte] {
    return func(yield func([]byte) bool) {
        for s.Scan() {
            if !yield(s.Bytes()) {
                break
            }
        }
    }
}

Reading whole file as collection of lines could be like:

    f, err := os.Open("file.txt")
    if err != nil {
        panic(err)
    }
    defer f.Close()
    scanner := bufio.NewScanner(f)

    // read all lines as slice of strings
    lines := slices.Collect(scanner.IterText())

    // instead of:
    // lines := make([]string, 0)
    // for scanner.Scan() {
    //  lines = append(lines, scanner.Text())
    // }
gabyhelp commented 15 hours ago

Related Issues

Related Documentation

Related Discussions

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

seankhliao commented 15 hours ago

this doesn't address how errors should be surfaced, see #70084 for related discussion

mateusz834 commented 15 hours ago

Also why global functions, instead of methods?

pkierski commented 15 hours ago

Also why global functions, instead of methods?

My bad, I've copied code from my experiments. I've thought about method ofc.

mateusz834 commented 14 hours ago

this doesn't address how errors should be surfaced, see https://github.com/golang/go/issues/70084 for related discussion

And #70631