fmt is Go’s standard-library package for formatted input and output. Import it after the package declaration, then call exported functions with the package-qualified form, such as fmt.Println. section 2.4.6 section 2.4.11
Choose the function that matches the output or input job:
- Use Print for adjacent output, Println for output with a trailing newline, and Printf for strings controlled by formatting verbs. section 10.1
- Use Sprint to build a string by concatenating arguments and Sprintf to build one from a format string. section 10.1
- Use Scanln for simple line input and Errorf for formatted errors that can be printed or returned. section 10.1
Formatting a conversion CLI
A small JSON/XML conversion tool can report its identity and version with `fmt.Printf("%s - version %d", app, version)`, prompt with `fmt.Print("Input json: ")`, read with `fmt.Scanln(&input)`, and reject empty input using `fmt.Errorf("invalid json: empty input")`. Common verbs include `%s` for strings, `%d` for decimal integers, `%v` for default values, `%T` for types, and `%.2f` for controlled float precision. section 10.1
Table 10.1 Common fmt escape codes
| Code |
Type |
Description |
Example output |
|---|---|---|---|
|
|
|
Default format |
|
|
|
|
Includes field names |
|
|
|
any |
Go syntax representation |
|
|
|
any |
Type of value |
|
|
|
|
Boolean |
|
|
|
|
Decimal integer |
|
|
|
|
Binary |
|
|
|
|
Hexadecimal (lowercase) |
|
|
|
|
Hexadecimal (uppercase) |
|
|
|
|
Decimal point (always six digits) |
|
|
|
|
Decimal with precision |
|
|
|
|
Scientific notation |
|
|
|
|
String |
|
|
|
|
Quoted string |
|
|
|
pointer |
Pointer address |
|
|
|
literal |
Percent sign |
|
Use the formatting-verbs table to select representations for values, structs, types, booleans, integers, floats, strings, pointers, and literal percent signs. section 10.1
Listing 10.4 main.go: Reading input and creating formatted errors
Use the runnable pattern to connect a prompt, Scanln input, empty-input validation, Errorf construction, and formatted confirmation. section 10.1


