100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

Input and Output in Go

Learn to print formatted output with fmt.Printf and read user input using fmt.Scanln and bufio.Reader.

BasicsBeginner9 min readJul 8, 2026
Analogies

Introduction

The standard library's fmt package provides the primary tools for console input and output in Go. Println and Printf handle output with different levels of formatting control, while Scan-family functions like Scanln read simple input. For more flexible line-based input, especially strings containing spaces, Go programs typically use bufio.NewReader wrapping os.Stdin.

🏏

Cricket analogy: fmt handling output while bufio.NewReader handles complex input is like a stadium PA system announcing scores plainly (output) while a separate detailed scorecard system captures nuanced player stats including multi-word names (input with spaces).

Syntax

go
fmt.Println("Hello, Gopher!")
fmt.Printf("Name: %s, Age: %d\n", name, age)

var name string
fmt.Scanln(&name)

reader := bufio.NewReader(os.Stdin)
line, _ := reader.ReadString('\n')

Explanation

fmt.Println prints values followed by a newline, automatically adding spaces between arguments. fmt.Printf gives precise control through format verbs: %d for integers, %s for strings, %f for floating-point numbers, %v for a value's default representation, and %T for the value's type. fmt.Scanln reads space-separated input into the addresses of variables (using the & operator) and stops at a newline, but it cannot handle input containing spaces within a single field. For reading a full line including spaces, wrapping os.Stdin with bufio.NewReader and calling ReadString('\n') is the idiomatic approach; the returned string usually needs strings.TrimSpace to remove the trailing newline character.

🏏

Cricket analogy: fmt.Printf's format verbs like %d and %s are like a scorecard template with dedicated columns for runs (integer) and player name (string), each formatted precisely rather than dumped as raw text.

Example

go
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)

	fmt.Print("Enter your full name: ")
	line, _ := reader.ReadString('\n')
	name := strings.TrimSpace(line)

	var age int
	fmt.Print("Enter your age: ")
	fmt.Scanln(&age)

	fmt.Printf("Hello %s, you are %d years old.\n", name, age)
}

Output

go
Enter your full name: Ada Lovelace
Enter your age: 36
Hello Ada Lovelace, you are 36 years old.

Key Takeaways

  • fmt.Println prints values with automatic spacing and a trailing newline.
  • fmt.Printf uses format verbs (%d, %s, %f, %v, %T) for precise output control.
  • fmt.Scanln reads space-separated values into variable addresses using &.
  • bufio.NewReader(os.Stdin).ReadString('\n') is the standard way to read a full line including spaces.
  • strings.TrimSpace is typically needed to strip the trailing newline from ReadString output.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#InputAndOutputInGo#Input#Output#Syntax#Explanation#StudyNotes#SkillVeris