O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required