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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

func Printf(format string, a ...any) (n int, err error)

Parameters

ParameterDescription
%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

Example 1
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"|ff

Run this on your computer — see “Get Started” for setup.

Sprintf and composite values
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.

Was this page helpful?