Overview

Appendix B. Formatting Cheat Sheet

This appendix is a compact reference to Go’s Printf-style formatting, showing how to turn values into text with a variety of verbs and flags. It highlights the default and type-aware representations alongside controls for width, precision, alignment, padding, and numerical bases. The emphasis is on practical outcomes—what a given verb prints for a given value—so developers can quickly pick the right specifier.

General verbs include %v for the default view, %#v for Go-syntax output, and %T for the value’s type. For integers, it covers decimal and sign handling (%d, %+d), field width and alignment (%4d, %-4d), zero padding (%04d), and base conversions (%b, %o, %x/%X, %#x). Floating-point formats span scientific (%e), fixed (%f) with adjustable precision (like %.2f) and width/padding (%8.2f, %08.2f), plus the compact %g. Character and text sections show runes and strings as raw or quoted (%c, %q), Unicode code points (%U, %#U), string alignment (%s, %8s, %-8s), and byte-level hex dumps for strings/byte slices (%x, % x). Booleans print with %t, and pointers use %p in 0x-prefixed hexadecimal.

It also catalogs special escape sequences for control characters such as alert, backspace, tab, newline, form feed, carriage return, vertical tab, and backslash, along with the way to print a literal percent using %%. All Unicode code points can be embedded in string literals via backslash escapes in four forms: \xHH (two hex digits), \ooo (three octal digits), \uHHHH (four hex digits), and \UHHHHHHHH (eight hex digits). An example demonstrates inserting a Unicode character in a normal string with a \u escape.

FAQ

What are the default formatting verbs to inspect values?%v prints the value in a default format; %#v prints a Go-syntax representation; %T prints the value’s type. Example with []int64{0, 1}: %v → [0 1], %#v → []int64{0, 1}, %T → []int64.
How do I print integers in different bases?Use %d (decimal), %b (binary), %o (octal), %x (hex lowercase), %X (hex uppercase). Add # to %x for a 0x prefix: %#x. Example with 15: %d → 15, %b → 1111, %o → 17, %x → f, %X → F, %#x → 0xf.
How can I always show the sign with integers?Use %+d. Example: fmt.Printf("%+d", 15) prints +15 (negatives keep their minus sign).
How do I pad or justify integers?Use a width with %d. %4d pads with spaces on the left (right-justified). %-4d pads on the right (left-justified). %04d pads with leading zeros. Example with 15: "%4d" → ␣␣15, "%-4d" → 15␣␣, "%04d" → 0015.
What’s the difference between %f, %e, and %g for floats?%f uses fixed-point (default precision 6). %e uses scientific notation. %g picks the more compact of %e or %f and omits unnecessary zeros/exponent. Example with 123.456: %f → 123.456000, %e → 1.234560e+02, %g → 123.456.
How do I control float precision, width, and zero-padding?Use %[width].[prec]f. Examples with 123.456: %.2f → 123.46, %8.2f → ␣␣123.46, %08.2f → 00123.46.
How do I format runes/characters and show their Unicode code points?%c prints the character, %q prints a quoted character, %U prints the Unicode code point (e.g., U+0041), and %#U prints both code point and character. Example with 'A': %c → A, %q → 'A', %U → U+0041, %#U → U+0041 'A'.
How do I format strings or byte slices (including hex dumps)?%s prints the string; %q prints a quoted string. Width and justification work: %8s (right), %-8s (left). For byte values as hex, use %x (no spaces) or % x (space-separated). Example "gophers": %s → gophers, %q → "gophers", %x → 676f7068657273, % x → 67 6f 70 68 65 72 73.
How do I print booleans and pointers?%t prints booleans as true or false. %p prints a pointer address in hex with a leading 0x. Example: fmt.Printf("%t", true) → true; fmt.Printf("%p", new(int)) → e.g., 0xc0000b2000.
What escape sequences and Unicode escapes can I use in strings?Common escapes: \a (bell), \b (backspace), \\ (backslash), \t (tab), \n (newline), \f (form feed), \r (carriage return), \v (vertical tab). To print a percent sign with formatting, use %%. Unicode escapes: \xHH (2 hex), \ooo (3 octal), \uHHHH (4 hex), \UHHHHHHHH (8 hex). Example: "... endear\u00e8d ...".

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn Go with Pocket-Sized Projects ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn Go with Pocket-Sized Projects ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn Go with Pocket-Sized Projects ebook for free