For those who need to count words and lines in text files, an easy approach for this matter is to use bufio.ScanWords and bufio.ScanLine in order to quickly solve the problem.

To count words:

ScanWords is a split function for a Scanner that returns each space-separated (checks unicode.IsSpace) word of text, with trimmed whitespace.

To count lines:

ScanLines is a split function for a Scanner that returns each line of text (separated by “\r?\n”). It returns also empty lines and the last line is returned even if it has no newline at the end.