Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท fmt Package

Go fmt width, precision and flags

Reference for width, precision and flags in Go format strings: pad and align text, control decimals, zero-fill numbers, use * for dynamic widths and argument indexes.

Between the % and the verb you can place flags, a width and a precision. Together they control padding, alignment, signs and the number of digits. The general form is %[flags][width][.precision]verb.

Width is the minimum number of characters (actually runes) the output occupies; shorter values are padded. Precision means something different per verb: the number of decimals for floats, the maximum number of runes for strings, the minimum digits for integers.

Go
fmt.Printf("|%8.2f|%-8s|%08d|\n", 3.14159, "go", 42)

Try it

Go Padding and alignment

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Dynamic widths and a table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
%[argIndex]flags width .precision verb
Part Description
width Minimum field width. %6d pads to 6 characters. * reads the width from an int argument.
.precision Floats: digits after the decimal point (%.2f); for %g the significant digits. Strings and byte slices: maximum runes or bytes. Integers: minimum digits. .* reads it from an argument.
- flag Left-justify (pad on the right).
0 flag Pad numbers with leading zeros instead of spaces. Ignored when - is present.
+ flag Always print a sign for numbers; for %q use ASCII-only output.
space flag Leave a space where a plus sign would go for numbers; for %x on strings put spaces between bytes.
# flag Alternate form: 0x for %#x, 0 for %#o, 0b for %#b, Go syntax for %#v, raw string quotes for %#q when possible.
[n] Argument index (1-based). Applies to the next verb, and also to * widths.

Edge cases:

  • A value longer than the width is never truncated by width; only precision truncates strings.
  • Width counts runes for strings, so %5s of "รฉ" pads with four spaces. It does not account for double-width East Asian characters or combining marks.
  • %5.2d on an integer pads the digits to at least two, not the float meaning.
  • Precision on %d with value 0 and precision 0 prints nothing.

Usage notes

  • Alignment is left-justified for text only with -. The default is right-justified for both numbers and strings.
  • %08.3f with a negative number puts the sign first, then zeros: -003.142.
  • Money and tables: %-10s %8.2f is the classic pair for a text column and a number column. For many columns, text/tabwriter is easier than counting characters.
  • Unicode: if you align text containing wide characters, width in runes will not line up on screen.
  • Dynamic width: %*d consumes two arguments: the width first, then the value. A negative width means left-justify.
  • Indexes reset order. After %[1]d, the next verb without an index uses argument 2.