Format Verbs
Go's fmt package uses format verbs to control how values are printed. %v is the default format that works with any type, and there are also type-specific verbs available.
Syntax
// Basic usage (used inside fmt.Printf, fmt.Sprintf)
fmt.Printf("%v\n", value) // Default format
fmt.Printf("%d\n", integer) // Decimal integer
fmt.Printf("%s\n", str) // String
fmt.Printf("%f\n", floatNum) // Floating-point number
// Specifying width and precision
fmt.Printf("%10d\n", 42) // Width 10, right-aligned
fmt.Printf("%-10d\n", 42) // Width 10, left-aligned
fmt.Printf("%010d\n", 42) // Width 10, zero-padded
fmt.Printf("%.2f\n", 3.14159) // 2 decimal places
fmt.Printf("%8.2f\n", 3.14) // Width 8, 2 decimal places
Format Verb Reference
| Verb | Description |
|---|---|
| %v | Default format. Prints values in a natural representation for their type. |
| %+v | Like %v, but includes field names when printing structs. |
| %#v | Prints the value in Go syntax (with package name and type name). |
| %T | Prints the type name of the value. |
| %d | Prints an integer in decimal (base 10). |
| %b | Prints an integer in binary (base 2). |
| %o | Prints an integer in octal (base 8). |
| %x / %X | Prints an integer in hexadecimal (lowercase / uppercase). |
| %e / %E | Prints a floating-point number in scientific notation. |
| %f | Prints a floating-point number in decimal notation (default precision: 6 digits). |
| %g / %G | Uses %e or %f, whichever is more compact for the value. |
| %s | Prints a string or byte slice. |
| %q | Prints a string as a double-quoted Go string literal. |
| %c | Prints the Unicode character corresponding to the integer value. |
| %t | Prints a boolean as true or false. |
| %p | Prints a pointer as a hexadecimal address. |
| %% | Prints a literal percent sign %. |
Sample Code
package main
import "fmt"
type Person struct {
Name string
Age int
}
func main() {
// General-purpose verbs
p := Person{"Alice", 30}
fmt.Printf("%v\n", p) // {Alice 30}
fmt.Printf("%+v\n", p) // {Name:Alice Age:30}
fmt.Printf("%#v\n", p) // main.Person{Name:"Alice", Age:30}
fmt.Printf("%T\n", p) // main.Person
fmt.Println()
// Integer in different bases
n := 255
fmt.Printf("Decimal: %d\n", n)
fmt.Printf("Binary: %b\n", n)
fmt.Printf("Octal: %o\n", n)
fmt.Printf("Hex (lower): %x\n", n)
fmt.Printf("Hex (upper): %X\n", n)
fmt.Println()
// Floating-point formatting
f := 3.14159265
fmt.Printf("Default: %f\n", f)
fmt.Printf("Precision: %.2f\n", f)
fmt.Printf("Scientific: %e\n", f)
fmt.Printf("Compact: %g\n", f)
fmt.Println()
// Width and alignment
fmt.Printf("|%10d|\n", 42) // Right-aligned
fmt.Printf("|%-10d|\n", 42) // Left-aligned
fmt.Printf("|%010d|\n", 42) // Zero-padded
// Special verbs
fmt.Printf("%q\n", "Hello, 世界") // "Hello, 世界" (escaped string literal)
fmt.Printf("%c\n", 65) // A (character code 65)
fmt.Printf("100%%\n") // 100% (%% prints a literal percent sign)
}
Notes
Format verbs are used everywhere from debug output to production logging. Use %v or %#v to inspect values of unknown type. For numeric formatting, combining %d or %f with width and precision flags is very powerful. To customize formatting for your own types, implement the String() string method (the Stringer interface) — it will be called automatically by %v and %s.
If you use a verb that does not match the value's type — for example, %s with an integer or %d with a string — Go prints a warning string like %!s(int=42). Run go vet to catch these mismatches before compiling.
For details on fmt.Printf(), see fmt.Println() / Printf(). For string formatting, see fmt.Sprintf() / Fprintf().
If you find any errors or copyright issues, please contact us.