Most programs end by showing a result, and the shape of that line matters: a report that reads Poems: 25 min is easier to scan than Poems25. The fmt package gives you two main tools. fmt.Println puts a space between its arguments and ends the line. fmt.Printf follows a format string instead: %s stands for a string, %d for an integer, and \n ends the line. Printf adds nothing you do not write, so the \n matters.
Println with several values
Println is the quick option. Give it several values separated by commas and it prints them in order, puts one space between each pair, and finishes with a newline. Values do not need to be the same type, so you can mix text and numbers freely. The trade-off is control: you always get exactly one space, so Println cannot produce Poems: 25 min without you building the colon into the text yourself.
package main
import (
"fmt"
)
func main() {
title := "Poems"
minutes := 25
fmt.Println(title, minutes, "min")
fmt.Println("Total:", minutes+10)
}Poems 25 min
Total: 35Predict and run
Printf reads its first argument, the format string, from left to right. Ordinary characters are copied as they are. Each % verb is a placeholder, replaced by the next argument in the list. Which values fill the two placeholders?
package main
import (
"fmt"
)
func main() {
fmt.Printf("%s: %d min\n", "Poems", 25)
}Check your prediction
Poems: 25 minAdd a second Printf line for "Essays" and 30. Then remove the \n from the first line and run again.
A few more verbs
You will use %s and %d most of the time, but a few others are worth knowing early:
%vprints any value in its default form. It is handy while exploring, though a specific verb documents your intent better.%qprints a string with quotes around it, which makes leading or trailing spaces visible.%%prints a literal percent sign, because a single%always starts a verb.
package main
import (
"fmt"
)
func main() {
title := " Poems "
fmt.Printf("%v|%v\n", title, 25)
fmt.Printf("%q\n", title)
fmt.Printf("%d%% of the goal\n", 50)
} Poems |25
" Poems "
50% of the goalThe first line hides the spaces around Poems unless you look closely. %q shows them at once. That will be useful on Day 5, when you clean up text typed by a user.
Width and alignment
A number between % and the verb sets a minimum width. %4d pads a number on the left to four characters, so the units line up in a column. A minus sign pads on the right instead: %-14s keeps the text on the left and fills the rest of the 14 characters with spaces. Together they turn a list into a readable table.
package main
import (
"fmt"
)
func main() {
fmt.Printf("%-14s%4d min\n", "Poems", 25)
fmt.Printf("%-14s%4d min\n", "Short stories", 135)
}Poems 25 min
Short stories 135 minBuild a string without printing it
fmt.Sprintf takes the same format string and arguments as Printf, but returns the finished text instead of printing it. Store the result in a variable, for example line := fmt.Sprintf("%s: %d min", title, minutes), when you want to print it later or pass it somewhere else.
When the arguments do not match
Printf cannot check your format string while it compiles, so mistakes show up in the output instead. Too few arguments print %!d(MISSING). A value of the wrong type prints something like %!s(int=30). If you see %! in your output, compare the verbs with the arguments one by one, from left to right.
Practice
Your turn
Store two sessions in four variables: Poems for 25 minutes and Essays for 5. Print them as a lined-up table with Printf, then print how much of a 60-minute goal the two sessions reach, as a percentage worked out from the variables. The output should be:
Poems 25 min
Essays 5 min
Goal: 50%package main
func main() {
// Write your code here.
}Hint 1: the idea
A width between % and the verb pads the value, and a minus sign pads on the right. A percent sign in the output needs %%, because a single % starts a verb.
Hint 2: a first step
Print the first row with fmt.Printf("%-14s%4d min\n", ...) and count the characters before 25. There should be 14 before the number's own four.
Worked solution
package main
import (
"fmt"
)
func main() {
firstTitle := "Poems"
firstMinutes := 25
secondTitle := "Essays"
secondMinutes := 5
fmt.Printf("%-14s%4d min\n", firstTitle, firstMinutes)
fmt.Printf("%-14s%4d min\n", secondTitle, secondMinutes)
percent := (firstMinutes + secondMinutes) * 100 / 60
fmt.Printf("Goal: %d%%\n", percent)
}Poems 25 min
Essays 5 min
Goal: 50%Multiplying by 100 before dividing matters. 30 / 60 * 100 is 0, because 30 / 60 drops everything after the point before the multiplication happens.
Completion is saved in this browser only. It does not measure mastery.