Skip to content
Tutorials Center
My learning

Topics

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.

Go
import "fmt"

fmt.Println("hello", 42)
msg := fmt.Sprintf("%s has %d items", name, n)

Try it

Go The three print flavors

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 Values, errors and Stringer

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

  • Println and Printf are for output meant for people. For logging use log or log/slog; they add timestamps and levels.
  • Print adds a space between operands when neither is a string, which surprises people. Prefer Println or Printf.
  • A type with a String() string method (or Error() string) is printed through that method by %v and %s. A method with a pointer receiver only applies to pointers.
  • The go vet tool checks Printf calls: 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 wrap os.Stdout in a bufio.Writer.
  • Sprintf allocates. For many small concatenations, strings.Builder or strconv are cheaper.
  • Printing maps sorts the keys, so output is deterministic. Printing pointers to structs shows &{...} at the top level only.
  • fmt uses reflection, so it works with any value but is slower than direct conversions such as strconv.Itoa.