Go 1.27 reference · Formatting, Strings and Text
Go fmt.Printf Function
Print formatted output using verbs like %v, %+v, %d, %s, %q, %.2f and %T, with width and alignment options.
Go 1.27
Syntax
func Printf(format string, a ...any) (n int, err error)
Parameters
| Parameter | Description |
|---|---|
%v / %+v / %#v |
Default / with field names / Go syntax. |
%d %s %q %x |
Integer, string, quoted, hex. |
%.2f %8.2f |
Precision and width. |
%-10s |
Left-aligned in 10 columns. |
%T |
The type. |
Examples
package main
import "fmt"
func main() {
type Order struct {
ID int
Total float64
}
o := Order{ID: 7, Total: 19.5}
fmt.Printf("%v | %+v | %T\n", o, o, o)
fmt.Printf("%-6s|%6.2f|%q|%x\n", "mug", 8.5, "tea", 255)
}
Output
{7 19.5} | {ID:7 Total:19.5} | main.Order
mug | 8.50|"tea"|ffRun this on your computer — see “Get Started” for setup.
package main
import "fmt"
func main() {
s := fmt.Sprintf("%05d-%s", 42, "A")
fmt.Println(s)
fmt.Printf("%t %v %v\n", true, []int{1, 2}, map[string]int{"a": 1})
}
Output
00042-A true [1 2] map[a:1]
Run this on your computer — see “Get Started” for setup.
Related
fmt.Errorf· Create an error with a formatted message; the %w verb wraps another error so errors.Is and errors.As can find it.strconv.Atoi, Itoa and ParseFloat· Convert between strings and numbers or booleans, with errors for invalid input.- Go Syntax · tutorial