In this post we will see some bufio.Scanner examples which comes built-in from go language itself.

Scanner Examples

In the code section i've share 3 example usage of bufio.Scanner 

  • Read word from standard input
  • Read line from standard input
  • Read from file

Some Quick References to understand the example code better:


NewScanner returns a new Scanner to read from r. The split function defaults to ScanLines.


Split sets the split function for the Scanner. The default split function is ScanLines.


Scan advances the Scanner to the next token, which will then be available through the Bytes or Text method