Go fmt Verb Reference

All Go fmt verbs with accepted types and output examples.

Searchable reference for Go fmt package format verbs — %v, %+v, %#v, %T, %d, %b, %o, %x, %f, %e, %g, %s, %q, %p — with accepted types, output examples, and width/precision flag notes.

What is the difference between %v, %+v and %#v?

%v prints a value in a default format. %+v adds field names when printing a struct. %#v prints a Go-syntax representation including the type name, suitable for pasting back into code.

The Go fmt package formats values with verbs — a percent sign followed by a letter — that control how each argument is rendered. Choosing the wrong verb gives misleading output or an explicit error placeholder. This tool is a searchable reference of every common verb with its accepted types and a worked example.

How it works

fmt.Printf, Sprintf, and friends scan the format string and pair each verb with the next argument. Each verb has a default behaviour plus optional flags, width, and precision that refine it. The reference groups verbs by purpose:

  • General%v (default), %+v (with field names), %#v (Go syntax), %T (type), %% (literal percent).
  • Integers%d (decimal), %b (binary), %o (octal), %x/%X (hex), %c (rune), %U (Unicode).
  • Floats%f/%F, %e/%E (scientific), %g/%G (compact).
  • Strings/bytes%s, %q (quoted), %x (hex bytes).
  • Boolean%t.
  • Pointer%p.

Worked example

type User struct{ Name string; Age int }
u := User{"Ada", 36}
fmt.Printf("%v\n", u)   // {Ada 36}
fmt.Printf("%+v\n", u)  // {Name:Ada Age:36}
fmt.Printf("%#v\n", u)  // main.User{Name:"Ada", Age:36}
fmt.Printf("%T\n", u)   // main.User
fmt.Printf("%6.2f\n", 3.14159) // "  3.14"
fmt.Printf("%q\n", "hi\n")     // "hi\n"

Notes

  • Flags: + always shows a sign, (space) leaves a space for the sign of positive numbers, - left-justifies, 0 pads with leading zeros, # enables the alternate form (0x prefix for %#x, etc.).
  • Width and precision can be supplied dynamically with *, taking the value from the argument list, as in fmt.Printf("%*d", 5, n).
  • A type mismatch produces a visible placeholder such as %!d(string=hi) instead of a panic, so bugs surface in the output.