build: golang.org/x/build/internal/foreach Index | Examples | Files

package foreach

import "golang.org/x/build/internal/foreach"

Package foreach provides allocation-conscious helpers for iterating over lines of text.

They're factored out into a separate small package primarily to allow them to have allocation-measuring tests that need to run without interference from other goroutine-leaking tests.

Index

Examples

Package Files

foreach.go

func Line Uses

func Line(v []byte, f func([]byte) error) error

Line calls f on each line in v, without the trailing '\n'. The final line need not include a trailing '\n'. Returns first non-nil error returned by f.

Code:

v := []byte(`line 1
line 2
line 3


after two blank lines
last line`)
foreach.Line(v, func(b []byte) error {
    fmt.Printf("%q\n", b)
    return nil
})

Output:

"line 1"
"line 2"
"line 3"
""
""
"after two blank lines"
"last line"

func LineStr Uses

func LineStr(s string, f func(string) error) error

LineStr calls f on each line in s, without the trailing '\n'. The final line need not include a trailing '\n'. Returns first non-nil error returned by f.

LineStr is the string variant of Line.

Package foreach imports 2 packages (graph) and is imported by 6 packages. Updated 2019-11-15. Refresh now. Tools for package owners.