The scanner's buffer size must be set to a value equal to or greater than the longest line in the file. The application cannot know this size when scanning files with unknown content and size.
If your your actual problem is counting the number of lines in a file, then count newlines in the file:
func countLines(r io.Reader) (int, error) {
buf := make([]byte, 4086)
lines := 0
sep := []byte{'\n'}
for {
n, err := r.Read(buf)
lines += bytes.Count(buf[:n], sep)
if err == io.EOF {
return lines, nil
} else {
return lines, err
}
}
}