Go Reference ยท fmt Package
The Go fmt package
Overview of Go's fmt package: the Print, Sprint, Fprint and Errorf families, formatting verbs, the Stringer interface, and the Scan functions for input.
The fmt package implements formatted input and output. It is the first package most Go programs import, and it gives you three groups of functions: printing (to standard output, a string or any io.Writer), error construction (Errorf), and scanning (reading values from text).
Every printing function comes in three flavors, which differ in where the text goes and how arguments are laid out. Once you know the naming scheme, the whole package is easy to navigate.
import "fmt" fmt.Println("hello", 42) msg := fmt.Sprintf("%s has %d items", name, n)
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
The naming scheme gives 4 x 3 functions:
| Destination | No format | Newline | Format string |
|---|---|---|---|
| standard output | Print |
Println |
Printf |
| returned string | Sprint |
Sprintln |
Sprintf |
io.Writer |
Fprint |
Fprintln |
Fprintf |
append to []byte |
Append |
Appendln |
Appendf |
Other pieces:
| Name | Description |
|---|---|
Errorf |
Builds an error from a format string; %w wraps another error. |
Scan, Scanln, Scanf |
Read space-separated values from standard input. |
Sscan, Sscanf, Sscanln |
Read from a string. |
Fscan, Fscanf, Fscanln |
Read from an io.Reader. |
Stringer, GoStringer |
Interfaces that control how a type prints with %v/%s and %#v. |
Formatter, State |
Full control over formatting for custom types. |
Scanner, ScanState |
Custom scanning behavior. |
Every printing function returns (n int, err error): the number of bytes written and a write error. Almost everyone ignores them for standard output.
Usage notes
PrintlnandPrintfare for output meant for people. For logging uselogorlog/slog; they add timestamps and levels.Printadds a space between operands when neither is a string, which surprises people. PreferPrintlnorPrintf.- A type with a
String() stringmethod (orError() string) is printed through that method by%vand%s. A method with a pointer receiver only applies to pointers. - The
go vettool checksPrintfcalls: wrong verbs, missing arguments and non-constant format strings are reported. Run it in CI. - Output to standard output is not buffered by
fmt; each call is a system write. In hot loops wrapos.Stdoutin abufio.Writer. Sprintfallocates. For many small concatenations,strings.Builderorstrconvare cheaper.- Printing maps sorts the keys, so output is deterministic. Printing pointers to structs shows
&{...}at the top level only. fmtuses reflection, so it works with any value but is slower than direct conversions such asstrconv.Itoa.