3. Printing and formatting
You'll print things constantly — to see results, to debug, to build output.
The fmt package gives you three workhorses. Learn them once and you'll
reach for the right one without thinking.
Println, Print, Printf
Println prints its arguments with spaces between them and a newline at
the end. It's the friendly default.
Print is the same but adds no trailing newline, and only puts spaces
between arguments that aren't strings. It's fussier — you'll use it less.
Printf ("print formatted") is the powerful one: you give it a
format string with % verbs as placeholders, then the values to slot
in. Run this and compare the three lines:
package main
import "fmt"
func main() {
name := "Grace"
age := 45
fmt.Println("Name:", name, "Age:", age)
fmt.Print("Name: ", name, " Age: ", age, "\n")
fmt.Printf("Name: %s, Age: %d\n", name, age)
}
With Printf you control the layout exactly, and you have to add \n
yourself — it won't guess.
The verbs you actually use
Each % verb is a slot that formats the matching argument a certain way:
| Verb | Meaning | Example output |
|---|---|---|
%s |
string | Grace |
%d |
integer (decimal) | 45 |
%f |
floating-point | 3.140000 |
%.2f |
float, 2 decimals | 3.14 |
%t |
boolean | true |
%v |
any value, default format | 45 |
%q |
quoted string | "Grace" |
%T |
the value's type | int |
%v is the "I just want to see it" verb — it prints almost anything
sensibly. %T is your best friend when you're unsure what type something is.
package main
import "fmt"
func main() {
pi := 3.14159
fmt.Printf("Rounded: %.2f\n", pi)
fmt.Printf("As a value: %v, and its type is %T\n", pi, pi)
fmt.Printf("A quoted string looks like %q\n", "hello")
}
Building a string instead of printing it
Sometimes you want the formatted text as a string — to store it or
return it — not print it. Sprintf ("string print formatted") is Printf
that hands you the string back:
package main
import "fmt"
func main() {
label := fmt.Sprintf("User #%d: %s", 7, "Katherine")
fmt.Println(label)
fmt.Printf("label is %d characters\n", len(label))
}
Sprintf is one of the most-used functions in all of Go. Any time you need
"a string with some values plugged into it," that's the tool.
A verb without an argument
If your format string has more verbs than arguments (or a typo), Go doesn't
crash — it prints a visible complaint like %!d(MISSING) right in the
output. Run this to see what a mistake looks like, so you recognize it later:
package main
import "fmt"
func main() {
fmt.Printf("Two numbers: %d and %d\n", 10)
}
Your turn
Using a single Printf, print exactly this line:
Item: Widget | Price: $9.50 | In stock: true
The price is a float shown to 2 decimals; the last value is a boolean.
package main
import "fmt"
func main() {
item := "Widget"
price := 9.5
inStock := true
// one Printf that produces the target line
}
package main
import "fmt"
func main() {
item := "Widget"
price := 9.5
inStock := true
fmt.Printf("Item: %s | Price: $%.2f | In stock: %t\n", item, price, inStock)
}
Next module: giving your values names — variables, types, and constants.