Formatting with fmt

The fmt package formats and prints values using verbs like %d , %s , and %v , giving you precise control over how numbers, strings, structs, and errors appear.

Learn Formatting with fmt in our free Go course — an interactive lesson with runnable examples, a practice exercise and a quick reference.

Part of the free Go course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ The Print Family

There are three families. Println separates arguments with spaces and adds a newline — perfect for quick prints. Printf takes a format string full of verbs and your values, giving exact control. Sprintf is identical to Printf but returns the string instead of printing it, so you can store or pass it around. (Plain Print adds no newline and only spaces between non-string operands — a common surprise.)

2️⃣ Verbs, Width & Precision

Verbs tell fmt how to render each value: %d integer, %s string, %f float, %v the default form, %+v a struct with field names, %#v Go-syntax, %T the type, and %q a quoted string. Add a width ( %5d ) to pad, a leading - to left-justify, and a precision ( %.2f ) for decimal places.

3️⃣ The Stringer Interface & Errorf

If a type has a String() string method it satisfies the fmt.Stringer interface, and fmt automatically uses it for %v and %s — your own pretty format, everywhere. Separately, fmt.Errorf builds an error from a format string, and the special %w verb wraps an existing error so it can later be matched with errors.Is or unwrapped.

🎯 Your Turn

Fill in the blank to build the string "Go v1.24" with %s and %.2f .

Print each item with the name left-justified in a 10-wide field and the price as $ with 2 decimals in a 6-wide field, so the columns line up. The verb is %-10s $%6.2f .

Practice quiz

Which Print function returns the formatted string instead of printing it?

  • fmt.Println
  • fmt.Printf
  • fmt.Sprintf
  • fmt.Print

Answer: fmt.Sprintf. Sprintf formats exactly like Printf but returns the result as a string.

What does fmt.Println("a", "b", 1) print?

  • a b 1
  • ab1
  • a, b, 1
  • ab 1

Answer: a b 1. Println puts a space between every argument and adds a trailing newline.

For a struct Point{1,2}, what does %v print?

  • {X:1 Y:2}
  • main.Point{X:1, Y:2}
  • main.Point
  • {1 2}

Answer: {1 2}. %v is the default format; for a struct it shows just the values: {1 2}.

Which verb adds the field names, printing {X:1 Y:2}?

  • %v
  • %+v
  • %#v
  • %T

Answer: %+v. %+v includes field names, the most useful for debugging structs.

What does %T print for a value?

  • its type
  • its address
  • its Go-syntax representation
  • its zero value

Answer: its type. %T prints the value's type, e.g. main.Point.

What does fmt.Printf("%d", 255) print?

  • ff
  • 11111111
  • 255
  • 0xff

Answer: 255. %d formats an integer in decimal: 255. %x gives ff and %b gives 11111111.

What does %q do to the string hi?

  • prints hi
  • prints "hi" (double-quoted)
  • prints HI
  • prints the length

Answer: prints "hi" (double-quoted). %q prints the string as a double-quoted Go literal with escaping.

What does fmt.Printf("%.2f", 3.14159) print?

  • 3.14159
  • 3.1
  • 3.142
  • 3.14

Answer: 3.14. The .2 precision rounds the float to two digits after the decimal point.

In %-10s, what does the leading minus do?

  • negates the number
  • left-justifies within the width
  • removes padding
  • right-justifies

Answer: left-justifies within the width. A leading - makes the value left-justified in the 10-wide field instead of right.

What does the %w verb in fmt.Errorf do?

  • prints a warning
  • formats a width
  • wraps another error so it can be unwrapped
  • writes to a file

Answer: wraps another error so it can be unwrapped. %w wraps an error, keeping it detectable later with errors.Is / errors.Unwrap.