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

Reader Examples

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

  • readLineFromStandardInput: Read string until new line
  • readTextUntilDelimiter: read until the delimiter, i.e semicolon in this example
  • readKeyPress: read one character at a time which can be used to intercept special key presses such as enter key.

Some Quick References to understand the example code better:

NewReader

NewReader returns a new Reader whose buffer has the default size.

ReadLine()

ReadLine is a low-level line-reading primitive. ReadLine tries to return a single line. The text returned from ReadLine does not include the line end ("\r\n" or "\n").

ReadString()

ReadString reads until the first occurrence of delim in the input, returning a string containing the data up to and including the delimiter.

ReadRune()

ReadRune reads a single UTF-8 encoded Unicode character and returns the rune and its size in bytes.