Created
April 5, 2025 05:31
-
-
Save wjkoh/c9e9bb2646de6347d417194a92d9674d to your computer and use it in GitHub Desktop.
Go: Line reader iterator (JSONL, NDJSON)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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