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

Functions

func Line

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.

Example

Code:play 

package main

import (
	"fmt"

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

func main() {
	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

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.

Source Files

foreach.go

Version
v0.0.0-20250421191922-3619c213cff3 (latest)
Published
Apr 21, 2025
Platform
linux/amd64
Imports
2 packages
Last checked
3 months ago

Tools for package owners.