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
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
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
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
1. Which format verb prints the type of a variable using fmt.Printf?
2. Why might fmt.Scanln fail to correctly capture a full name like 'Ada Lovelace'?
3. What is the idiomatic way to read a full line of input including spaces in Go?
4. What must you pass to fmt.Scanln to store input into a variable?
Was this page helpful?
You May Also Like
Variables and Data Types in Go
Learn how Go declares variables with var and :=, and the built-in types like int, float64, bool, and string.
Type Conversion in Go
Learn why Go requires explicit type conversion between numeric and other types, and how to perform it safely.
Error Handling in Go
Learn Go's idiomatic error handling using the built-in error interface and explicit error return values instead of exceptions.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics