How it works...
The stdin of the Go process could be retrieved via the Stdin of the os package. In fact, it is a File type which implements the Reader interface. Reading from the Reader is then very easy. The preceding code shows three very common ways of how to read from the Stdin.
The first option illustrates the use of the fmt package, which provides the functions Scan, Scanf, and Scanln. The Scanf function reads the input into given variable(s). The advantage of Scanf is that you can determine the format of the scanned value. The Scan function just reads the input into a variable (without predefined formatting) and Scanln, as its name suggests, reads the input ended with the line break.
The Scanner, which is the second option shown in ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access