A format verb is a directive in formatted I/O that controls how a value becomes text. Go uses verbs with fmt functions such as Printf and Sprintf, giving output predictable type-specific representations instead of requiring manual conversion. section 10.1
Format verbs also make operational messages clearer. Annotating an error with surrounding text identifies the failed operation while %s or %v invokes the error's Error method to obtain its message. section 7.6
Common verbs support these practical output tasks:
- %s formats strings, %v uses a default representation for any value, and %T prints a value's type. section 10.1
- %d prints decimal integers, while %b, %x, and %X print binary, lowercase hexadecimal, and uppercase hexadecimal forms. section 10.1
- %f and %.2f format floating-point values with default or specified precision; %q quotes strings. section 10.1
- %+v includes struct field names, %#v produces Go syntax, and %% emits a literal percent sign. section 10.1
Versioned output and annotated errors
A CLI can combine values with their intended representations: fmt.Printf("%s - version %d", app, version). When a store operation fails, log.Printf("unable to place value: %v", err) preserves the error while adding the failed operation. section 7.6 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 fmt verb table to compare value types, representations, precision controls, and example output. section 10.1
