Skip to content

Instantly share code, notes, and snippets.

@djale1k
Last active January 7, 2025 15:43
Show Gist options
  • Save djale1k/a328bbb96e26ec304d320f60e8c5e87a to your computer and use it in GitHub Desktop.
Save djale1k/a328bbb96e26ec304d320f60e8c5e87a to your computer and use it in GitHub Desktop.
Count file lines fast in Go
func LineCounter(r io.Reader) (int, error) {
buf := make([]byte, 32*1024)
count := 0
lineSep := []byte{'\n'}
for {
c, err := r.Read(buf)
count += bytes.Count(buf[:c], lineSep)
switch {
case err == io.EOF:
return count, nil
case err != nil:
return count, err
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment