Reading Standard Input in Go: A Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Read from Standard Input in Go: From Beginner to Pro



Have you ever wondered how to make your Go program "listen" to the user? How to write a utility that accepts commands from the console, or an interactive CLI tool? If so, you've come to the right place. Today we'll break down reading from standard input (stdin) in Go — in as much detail as possible.



This topic is a real hit on Stack Overflow. The question "How can I read from standard input in the console?" has gathered thousands of views. And for good reason: standard input is a fundamental concept that no serious Go project can do without. We'll adapt this question for a Russian-speaking audience and turn it into a full-fledged tutorial.



In this article, you will learn:

  • What os.Stdin is and how to work with it
  • How to use bufio.Scanner for line-by-line reading
  • Why fmt.Scan is a double-edged sword
  • How to read entire files from stdin
  • Secrets of error handling and performance


Let's go!



What is stdin and why do you need it in Go?



Standard input (stdin) is a data stream that your program receives from the console or from another process. In Go, it is represented by the variable os.Stdin of type *os.File. It is the gateway through which data enters your program.



Why is this needed? Imagine:

  • You are writing a CLI utility that processes user input
  • You are creating a log parser that reads data via a pipe (cat log.txt | myapp)
  • You need to implement interactive input with prompts


Go, being a language for systems programming, provides elegant tools for working with stdin. Let's break them down in order.



Method 1: bufio.Scanner — The King of Line-by-Line Reading



The most popular and recommended way to read from stdin in Go is bufio.Scanner. It is simple, efficient, and solves 90% of tasks.



Here is what a classic example looks like:



package main

import ( "bufio" "fmt" "os")

func main() { scanner := bufio.NewScanner(os.Stdin) fmt.Println("Enter text (Ctrl+D to finish):") for scanner.Scan() { line := scanner.Text() fmt.Println("You entered:", line) } if err := scanner.Err(); err != nil { fmt.Fprintln(os.Stderr, "Read error:", err) }}


How does it work?

  • bufio.NewScanner(os.Stdin) — creates a scanner that reads from standard input
  • scanner.Scan() — returns true as long as there is data (by default, up to the newline character)
  • scanner.Text() — returns the current line without the character
  • The loop stops at EOF (Ctrl+D on Linux/Mac, Ctrl+Z on Windows)


This code is the foundation for any console input in Go. It works great with pipes and interactive input.



Customizing the Scanner



By default, Scanner splits input by lines. But you can change this:



// Reading by wordsscanner.Split(bufio.ScanWords)

// Reading by bytesscanner.Split(bufio.ScanBytes)

// Reading by characters (runes)scanner.Split(bufio.ScanRunes)


You can also set a custom splitter — for example, for CSV or custom delimiters. But that's a topic for another article.



Method 2: fmt.Scan — Quick and Dirty



If you need to input a single value — a number, a string, or several variables — fmt.Scan comes to the rescue. It's like scanf in C, only

Blogs

Book Recommendations