By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Every program you have written so far did the same thing every time it ran. A command-line tool is different: the user tells it what to do, and the program reads that request before it acts. Go gives you three ways to receive input: the raw arguments in os.Args, named options through the flag package, and lines of text from standard input with bufio.Scanner.
You will use all three in Penny. Arguments carry the command and its values, flags carry options such as -limit 5, and stdin is useful when the input is a stream of lines.
os.Args: What the Shell PassedWhen a program starts, the operating system hands it a slice of strings called os.Args. The first element, os.Args[0], is the name of the program. The rest are the words the user typed after it, already split on spaces. Skip index 0 and the remaining slice os.Args[1:] is your command line.
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println("argument count:", len(os.Args))
if len(os.Args) < 2 {
fmt.Println("usage: penny <command>")
return
}
fmt.Println("command:", os.Args[1])
if len(os.Args) > 2 {
fmt.Println("rest:", os.Args[2:])
}
}Run this program with go run . add 12.50 Coffee and the output changes to argument count: 4, command: add, and rest: [12.50 Coffee]. Notice that 12.50 arrives as the string "12.50", not as a number. Arguments are always text; converting them is your job.
flag Package for Named OptionsSometimes arguments are options rather than commands. The flag package parses -name value pairs, gives each option a default, and produces a help screen for free. You declare each flag, call flag.Parse(), then read the value through a pointer.
package main
import (
"flag"
"fmt"
)
func main() {
limit := flag.Int("limit", 3, "how many expenses to show")
verbose := flag.Bool("verbose", false, "print extra detail")
flag.Parse()
fmt.Println("limit:", *limit)
fmt.Println("verbose:", *verbose)
fmt.Println("leftover args:", flag.Args())
}flag.Int returns a *int pointing at the parsed value, which is why the program prints *limit with a star. Run the program with go run . -limit 5 -verbose and the output becomes limit: 5, verbose: true, leftover args: [].
| Call | Reads | Use it for |
|---|---|---|
flag.Int("limit", 3, "...") | A whole number | Counts, limits, IDs |
flag.String("file", "penny.json", "...") | Text | File names, descriptions |
flag.Bool("verbose", false, "...") | true or false | On/off switches |
flag.Parse() | The real command line | Call it once, before reading values |
flag.Args() | Everything that was not a flag | Positional arguments |
Standard input is the stream a program receives when the user types, or when another program pipes a file into it. bufio.NewScanner reads it one line at a time. Scan() moves to the next line and returns true while a line was read; Text() returns that line.
The scanner loop is the shape you will write again and again:
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
fmt.Println("Type an expense per line. Finish with Ctrl+D (macOS/Linux) or Ctrl+Z then Enter (Windows).")
scanner := bufio.NewScanner(os.Stdin)
count := 0
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
if line == "quit" {
break
}
count++
fmt.Println("added:", line)
}
if err := scanner.Err(); err != nil {
fmt.Println("read error:", err)
return
}
fmt.Println("expenses read:", count)
}A scanner loop must stop when the input ends. The idiomatic Go way is the one above: for scanner.Scan() checks the return value, and the loop ends automatically when there is nothing left to read. An empty stream, a closed pipe, or a user pressing the end-of-input key all produce the same signal: Scan() returns false.
This detail matters for real programs. If you write for { scanner.Scan() } and ignore the result, the program spins forever on empty input, burning a processor and never printing a thing. The loop condition is not decoration; it is the exit.
Change the scanner program so it keeps a running total: parse each line with strconv.Atoi, add it to total, and print the total at the end. Run it with no input first (the total is 0), then run it with a small file containing one number per line, using go run . < numbers.txt. Compare the two results.
Key takeaways
os.Args[0] is the program name; the arguments start at index 1.
Arguments arrive as strings, so convert them before doing arithmetic.
The flag package parses named options with defaults and a free help screen.
bufio.Scanner reads stdin line by line with Scan() and Text().
for scanner.Scan() stops at end of input, so an empty stdin ends the program.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.