Tags: #go #golang #line #counter
// Alternative line counter that benefits from assembly optimized functions
// offered by the bytes package to search characters in a byte slice.
func lineCounter(r io.Reader) (int, error) {
// my file 'lines' are ~80 bytes in length
buf := make([]byte, 80*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
}
}
}