Console Input & Output in Go
A beginner-friendly Go tutorial on how to print output in the terminal and take user input (text and numbers) using `fmt`, `os.Args`, and `bufio`.
Console Output Basics
Console output means showing text on the terminal screen. Go provides simple functions in the `fmt` package like `fmt.Println()` to print messages and values. These are extremely useful for debugging and building CLI apps.
1. fmt.Println() (Normal Output)
Use this when you want to print something and automatically go to the next line. Every `fmt.Println()` ends with a newline.
2. Printing Variables (fmt.Printf)
Use `fmt.Printf()` with format verbs like `%s` for strings and `%d` for integers to print values inside a format string.
3. Printing to stderr (Warnings and Errors)
Normal output goes to stdout. For warnings or errors, write to `os.Stderr` so tools can separate them from normal output.
Printing Without Newline
Normally `fmt.Println()` always adds a newline. For CLI tools, sometimes you want to print progress like "Loading..." on the same line. In Go, use `fmt.Print()` which does not add a newline.
1. fmt.Print() (No Newline)
This prints exactly what you give it — without adding a newline automatically.
2. Creating a Simple Progress Effect
Using a loop and `time.Sleep()` to print dots every few milliseconds (like real CLI tools).
Format Verbs and Advanced Formatting
Format verbs tell `fmt.Printf` how to display each value. Each verb has a letter (e.g. `s` for string, `d` for decimal integer). You can add width, precision, and flags for alignment and zero-padding. Learning these helps you print output exactly how you want.
1. Common Format Verbs (%s, %d, %f, %t, %v)
The most used verbs: strings, integers, floats, booleans, and a default representation.
2. Width and Precision
Add a number for minimum width. For floats, use width.precision (e.g. %6.2f for 2 decimal places in a 6-character field).
3. Advanced Verbs (%+v, %#v, %q, %T)
Useful for debugging and showing values in a precise or quoted form.
Formatted Console Output
Many CLI programs print clean tables. In Go, use `fmt.Printf` with width specifiers: a number before the verb sets the minimum width. Use `-` for left alignment (e.g. `%-15s`); without `-`, numbers are right-aligned (e.g. `%10d`).
1. Format Width and Alignment
Put a number between `%` and the verb for minimum width. Use `0` with numbers for zero-padding; use `-` for left alignment.
2. Print a Simple Table (Aligned Columns)
This prints rows in a fixed-width layout so everything looks clean.
Console Input (Reading User Data)
Console input means the user can type something in the terminal and your program reads it. Go supports: 1) `os.Args` for command-line arguments (like flags and positional args), and 2) reading from `os.Stdin` with `bufio` for interactive input.
1. os.Args (Command-Line Arguments)
Example: user runs `go run main.go John 22` and your code reads those values.
2. Interactive Input (bufio.Scanner)
The program asks a question and waits for the user to type and press Enter.
3. Scan a Number (Single Number Input)
Input from the user is always a string. Convert it to a number with `strconv.Atoi` and check for errors.
4. Scan Multiple Numbers (Space Separated)
Common in CLI: user types `10 20 30`. We split the string and convert each part to a number.