liveBook · Manning

Standard input

As explained in Learn Go with Pocket-Sized Projects

Standard input is the conventional input stream from which a command-line process reads textual or other data.

Standard input is the conventional stream a command-line process reads from. In a terminal game, it carries the player’s typed guess after Enter is pressed. section 5.0 section 5.1.2

A Go program can turn terminal input into a validated game value through a short pipeline:

  • Pass os.Stdin to the game constructor as an io.Reader. section 5.1.2
  • Wrap that reader with bufio.NewReader for buffered line reading. section 5.1.2
  • Call ReadLine to obtain one attempted guess at a time. section 5.1.2
  • Convert the input to []rune so character counts support Unicode words. section 5.1.2
  • Report read errors or invalid lengths, then continue prompting until the guess is acceptable. section 5.1.2

Connecting a terminal to a game

g := gordle.New(os.Stdin) g.Play() The game reads a line such as "apple" and accepts it when its rune length matches the configured solution length; a four-character input is rejected. section 5.1.2

Listing 5.5 game.go: Using Game with a reader
// Game holds all the information we need to play a game of gordle.
type Game struct {
    reader *bufio.Reader
}

The listing shows the Game structure retaining a *bufio.Reader, making standard input an explicit dependency of the game. section 5.1.2

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains line-oriented reading with bufio.Reader, error handling, rune conversion, and length validation, but defers broader input methods.
Examples
Demonstrates Gordle guesses such as HELLO, مرحبا, こんにちは, and invalid four-character input.
What it assumes
Assumes command-line execution and keyboard input, while introducing io.Reader as the input abstraction.
What you can do after
Apply it via Go’s os.Stdin and bufio.NewReader to read and validate terminal guesses.
How it connects
Connects standard input to os.Stdin, io.Reader, bufio.Reader, Unicode, and runes.

Also explained in

Compare how 5 books explain standard input →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.