Ad space (banner)
๐ŸนGo Lessons
Lesson 58 / 68

Reading from Standard Input

In this lesson you will learn how to read standard input with bufio.NewReader in Go, so your program can respond to what a user types. This is for anyone searching for "Go standard input how to use".

bufio.NewReader(os.Stdin) reads one line of input from the keyboard or another source. Use it when a command-line tool needs the user to type something.

The sample code prompts with fmt.Print("Enter your name: "), reads up to the newline with reader.ReadString('\n'), and strips the trailing newline with strings.TrimSpace(name).

A common stumbling block for beginners is using the string with the trailing newline still attached. Since the newline remains, removing it with strings.TrimSpace is the standard move.

In real-world development, using input without validating it can lead to unexpected errors, so pairing it with a validation step is safer.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Go directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)