Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท fmt Package

Go fmt.Printf() function

Reference for Go fmt.Printf(): format strings, the most useful verbs, width and precision, argument indexes, error markers like %!d and go vet checks.

fmt.Printf formats its arguments according to a format string and writes the result to standard output. Plain text in the format string is copied, and each verb that starts with % consumes the next argument and formats it. Unlike Println, Printf adds no newline and no spaces unless you write them.

Verbs encode both the kind of formatting (%d decimal, %s string, %x hex, %q quoted) and, with flags, width, precision and alignment. The full list is on the fmt verbs page.

Go
fmt.Printf("%s is %d years old\n", "Ann", 31)

Try it

Go Common verbs

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 Mistakes are reported inline

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
func Printf(format string, a ...any) (n int, err error)

A verb has the shape %[flags][width][.precision]verb:

Part Description
flags + always show sign; - left-align; # alternate form (0x, #v Go syntax); space leaves a space for the sign; 0 pad with zeros.
width Minimum number of characters, or * to take it from an argument.
precision After a dot: digits after the decimal point for floats, maximum runes for strings, minimum digits for integers.
verb The conversion: v d s q x X o b c U f e g t p T %.
Name Description
format The format string.
a The values to format, in order.

Returns the number of bytes written and any write error.

Errors and edge cases

  • Mistakes do not panic or stop the program. Instead the output contains markers: %!d(string=text) for a wrong type, %!d(MISSING) for a missing argument, %!(EXTRA int=2) for extra ones, and %!z(int=1) for an unknown verb.
  • % followed by nothing at the end gives %!(NOVERB).

Usage notes

  • Run go vet (it also runs during go test) to catch wrong verbs and argument counts at build time.
  • Use %v when you do not care about the exact format; use %+v for structs with field names and %#v for Go source syntax. %T prints the type.
  • %q quotes strings and runes safely, which is useful when whitespace matters in logs.
  • Padding counts runes for strings but bytes would be wrong for multi-byte text; for alignment with wide characters (CJK, emoji) widths can still look off in a terminal.
  • %s on a type with String() or Error() calls that method. %d on a type with String() still prints the number.
  • A literal percent sign is %%. A non-constant format string such as Printf(userInput) is a bug (and a vet warning); write Printf("%s", userInput) or Print(userInput).
  • Printf does not append a newline. Forgetting \n is a classic mistake.
  • Floats: %g picks the shortest representation, %f fixed, %e scientific; %v for floats uses %g.