Dev Skills

Printing and format verbs

The three printing functions, and the verbs you will use every day.

Topic 3 of 98 · 5 min to read · 45 min to build
After this topic you can
  • Choose between Print, Println, Printf and Sprintf for a given output.
  • Predict the output of Printf with %v, %T, %q, %.2f and %5d.
  • Write a literal percent sign and a padded column with format verbs.

The three printing functions

output.go
name, age := "Sahdev", 25
fmt.Print("no newline, no spaces between strings")
fmt.Println("adds spaces and a newline:", name, age)
fmt.Printf("formatted: %s is %d\n", name, age) // Printf needs its own \n
s := fmt.Sprintf("%s is %d", name, age) // returns the string instead of printing it
fmt.Println(s)
`Sprintf` builds a string and hands it back to you. The `S` prefix means "give me a string" across the whole fmt package: `Sprint`, `Sprintln`, `Sprintf`.

Formatting verbs

VerbWhat it printsExample output
%vthe value in its default form — your default choice25
%Tthe type of the valueint
%dan integer25
%sa stringSahdev
%qa quoted string — shows whitespace clearly"Sahdev"
%f / %.2fa float / to two decimal places3.140000 / 3.14
%ta booltrue
%xhexadecimal1f
%5d / %-5spad to 5 wide, right / left aligned 25 / ab
%%a literal percent sign%

Verbs you will meet later

  • %+v and %#v print a struct with its field names. A struct is a value made of several named fields; you will learn it properly in Structs.
  • %p prints a memory address. You will learn this in Pointers.
  • %w wraps one error inside another, and only works in fmt.Errorf. You will learn this in Error values.
  • fmt.Fprintln prints to a place you choose, such as the error stream os.Stderr, instead of the screen. It comes up in Interfaces.

Try it yourself

press Run — this executes on the Go Playground
package main
import "fmt"
func main() {
name, age, height, student := "sahdev", 25, 1.72, true
fmt.Printf("%v %v %v %v\n", name, age, height, student) // %v works for anything
fmt.Printf("%T %T %T %T\n", name, age, height, student) // the types
fmt.Printf("%s is %d years old and %.2f m tall\n", name, age, height)
fmt.Printf("student? %t (%.1f%% done)\n", student, 62.5)
fmt.Printf("%q vs %s\n", " spaces ", " spaces ")
fmt.Printf("[%5d] [%-6s]\n", age, name)
}
`%v` prints anything sensibly; `%T` tells you what type Go thinks a value is. Those two cover most debugging.
The short version
  • Println for quick output, Printf for formatted, Sprintf to build a string.
  • %v is the default for any value, %T shows the type.
  • Printf needs its own \n; Println adds one for you.
  • The S prefix (Sprintf, Sprintln) returns a string instead of printing it.
Remember: %v prints anything and %T tells you its type. Learn them before you learn a debugger.
Build this before moving on
Print a string, an int, a float and a bool with %v and then with %T, and compare the outputs.
Then try these in your own editor
  1. 1Build a sentence with Sprintf, store it in a variable, then print it with Println.
  2. 2Format a float to two decimal places, and print a literal percent sign.
  3. 3Print a string with %s and %q and see which one shows the whitespace.
Check yourselfTake the Getting started test