Exercise 1 of 10 · Variables and printing
Library Due-Date Reminder
What you will make
A two-line console message that reminds a borrower which book is due and how many days they have left.
The one new idea: Declaring variables with := and printing them with fmt.Println and fmt.Printf, including a format string that must end with \n
Almost every Go program starts by holding a few values in variables and showing something to the person running it — getting Printf's newline habits right now saves confusion later.
Go straight to the code ↓Every Go program that talks to the person running it leans on two workhorses from the fmt package: Println, which prints its arguments separated by spaces and always finishes with a newline, and Printf, which prints exactly the characters in a format string, substituting %s, %d, and friends for the arguments that follow. Printf is more precise, but that precision cuts both ways — it will not add a newline for you. If you want one, you type \n yourself, right where you want the line to end.
Worked example
Here's the same idea with a different book and a different number of days:
package main
import "fmt"
func main() {
book := "Charlotte's Web"
daysLeft := 1
fmt.Println("Library reminder")
fmt.Printf("\"%s\" is due in %d day(s)\n", book, daysLeft)
}Library reminder
"Charlotte's Web" is due in 1 day(s)Notice the format string ends in \n, matching the newline that Println added automatically on the line above it. Drop that \n and the two lines of output would visually smear together (or, in an automated grader, the expected output simply wouldn't match).
Your turn
The starter program declares a book title and a day count, prints a header, then prints the reminder itself — but the reminder line is missing something Println gives you for free. Run it, look at what's missing compared to the worked example above, and fix the one marked line so the program's output ends the same way.
If something goes wrong
If your output looks right in the terminal but still fails an exact comparison, check for a missing or extra \n first — that's invisible until you look closely. If you get a compile error mentioning book or daysLeft, make sure you didn't accidentally delete the := that declares them; Go variables can't be used before they're declared. And if the quotes inside the format string look confusing, remember \" is just an escaped double quote so Go knows it's part of the string, not the end of it.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Forgetting that fmt.Printf never adds a newline on its own.
- Unlike Println, Printf writes only what you put in the format string, so if you want a line break you have to type \n yourself.
- Mixing up %s and %d for the wrong argument.
- Printf verbs are matched to arguments in order and by type; putting %d where a string is expected prints an ugly error placeholder instead of the value.
- Using = instead of := the first time a variable is created.
- = only assigns to a variable that already exists; a brand-new variable needs := or the compiler reports it as undefined.
- Leaving a stray or mismatched quote in a string literal.
- Go strings must open and close with matching double quotes, and an escaped quote inside needs a backslash, or the file won't compile.
Want a blank editor instead? Open the Go playground.