Skip to content

Instantly share code, notes, and snippets.

@wjkoh
Created April 5, 2025 05:31
Show Gist options
  • Save wjkoh/c9e9bb2646de6347d417194a92d9674d to your computer and use it in GitHub Desktop.
Save wjkoh/c9e9bb2646de6347d417194a92d9674d to your computer and use it in GitHub Desktop.
Go: Line reader iterator (JSONL, NDJSON)
func Lines(f io.Reader) iter.Seq2[[]byte, error] {
r := bufio.NewReader(f)
return func(yield func([]byte, error) bool) {
for {
data, err := r.ReadBytes('\n')
if errors.Is(err, io.EOF) {
return
}
if !yield(data, err) {
return
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment