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.
fmt.Printf("%s is %d years old\n", "Ann", 31)
Try it
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.
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
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 duringgo test) to catch wrong verbs and argument counts at build time. - Use
%vwhen you do not care about the exact format; use%+vfor structs with field names and%#vfor Go source syntax.%Tprints the type. %qquotes 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.
%son a type withString()orError()calls that method.%don a type withString()still prints the number.- A literal percent sign is
%%. A non-constant format string such asPrintf(userInput)is a bug (and a vet warning); writePrintf("%s", userInput)orPrint(userInput). Printfdoes not append a newline. Forgetting\nis a classic mistake.- Floats:
%gpicks the shortest representation,%ffixed,%escientific;%vfor floats uses%g.