Skip to content

Checkpoint: Combine Two Sessions

The Course
•
24 min study

A checkpoint is a short task that pulls the whole day together. There is no new syntax here: everything you need appeared in the last three lessons. Try it without scrolling back first. If you get stuck, the hints come in two steps, so you can take only as much help as you need before the worked solution.

What you will use

  • A program skeleton. package main, an import for fmt, and func main, where your statements go.
  • Variables. := declares a name and infers its type from the value. Each variable you declare must be used.
  • Arithmetic. + adds two int values, and you can write the sum directly as an argument.
  • Formatted output. fmt.Printf copies its format string, replaces %d with the next whole-number argument, and prints a newline only where you write \n.

Plan before you type

Break the task into small steps you can check one at a time. A useful habit is to say each step in plain words before writing it in Go:

  1. Make a name for the first session and give it its value.
  2. Make a name for the second session.
  3. Work out the total from those two names, not from numbers you added in your head.
  4. Print the total inside the sentence the task asks for.

Run the program after the first two steps, printing each variable with fmt.Println. Seeing the values early tells you the setup is right before you build the final line. Once the line is correct, take out the extra Println calls you added along the way.

Let the program do the sum

It is tempting to type Total: 40 min straight into the format string, because you already know the answer. The program would print the right line today and the wrong line as soon as a session changes. The task asks you to change the numbers afterwards for exactly this reason: a total that does not follow the inputs is not really a total.

Mistakes worth checking for

  • The output runs into the next line, or the page shows no line break at all: the format string is missing \n.
  • The output shows %!d(MISSING): the format string has a verb but no argument to fill it.
  • The compiler reports declared and not used: you made a variable but never passed it to anything.
  • The compiler reports undefined: fmt: the import line is missing.
Exercise

Your turn

Store two sessions of 50 and 25 minutes in variables. Print the total with one Printf call, then the same total as hours and minutes with a second one. Change either number and check that both lines follow. The output should be:

Text
Total: 75 min
That is 1 h 15 min
Your working space Go
package main

func main() {
	// Build your solution here.
}
Hint 1: the idea

Compute the sum from the two variables, and keep it in a variable of its own: both lines use it. An hour is 60 minutes, and Go's whole-number division keeps the part you need for each half of the second line.

Hint 2: a first step

Print the first line and check it says 75. Then work out the hours alone and print them, before you add the leftover minutes.

Worked solution
Run the solution Go
package main

import (
	"fmt"
)

func main() {
	first := 50
	second := 25
	total := first + second
	fmt.Printf("Total: %d min\n", total)
	fmt.Printf("That is %d h %d min\n", total/60, total%60)
}
Text
Total: 75 min
That is 1 h 15 min

The sum comes from the variables, so changing either one changes both lines. Writing 75 directly would not. total/60 keeps only the whole hours, and total%60 gives the minutes left over.

Before Day 2

Check that you can do each of these without looking back:

  • Explain why minutes := 8 fails when minutes already exists, and what to write instead.
  • Predict what fmt.Println("Total:", 40) prints, including the space.
  • Line up two titles in a column with %-14s.

Day 2 adds decisions and repetition: if to choose between paths, and for to repeat work while a running total grows.

Was this article helpful?

Your response is saved on this device.