Skip to content

Checkpoint: Total Minutes by Title

The Course
•
24 min study

This checkpoint is a small version of the report the reading log will produce on Day 7. You start with a list of individual sessions and end with one total per title. Two titles share a name, so the map has to combine them. The output must also come out in the same order every time, even though the map has no order of its own.

What you will use

  • A struct type. Session has a Title field of type string and a Minutes field of type int. Declare it outside main.
  • A slice of structs. One record per line, with named fields and a comma after each record.
  • A map built with make. Its key type matches the title and its value type matches the minutes.
  • The zero value of a missing key. totals[s.Title] += s.Minutes works the first time a title appears, because the missing key reads as 0.
  • Explicit lookups for output. Asking for totals["Poems"] and then totals["Essays"] fixes the order yourself.

Plan before you type

  1. Declare the Session type with its two fields.
  2. In main, build the slice with the three records in the order the task gives.
  3. Make an empty map from titles to minutes.
  4. Range over the slice and add each record's minutes to the entry for its title.
  5. Print the Poems total, then the Essays total, each on its own line.

Predict the two numbers before running. Poems appears twice, at 10 and 15 minutes, so its total combines both. Essays appears once.

Check the map before printing from it

While you work, fmt.Println(totals) shows the whole map with sorted keys, so you can confirm both totals at once. The final program prints by key instead, because the task fixes the order and asks for numbers only.

Mistakes worth checking for

  • The program stops with assignment to entry in nil map: the map was declared with var but never created with make.
  • The Poems total is 15, not 25: the code used = instead of +=, so the second Poems record replaced the first.
  • The order changes between runs: the output comes from a range loop over the map instead of two explicit lookups.
  • The compiler reports undefined: Session: the type declaration is missing, or it sits inside main below the line that first uses it.
Exercise

Your turn

Make a slice of Session records: Poems 10, Essays 20 and Poems 15. Add each duration into a map keyed by title, then print the total for Poems and then the total for Essays.

Your working space Go
package main

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

Map order is not fixed, so look up each key yourself instead of ranging over the map.

Hint 2: a first step

Use each record's Title as the map key.

Worked solution
Run the solution Go
package main

import (
	"fmt"
)

type Session struct {
	Title   string
	Minutes int
}

func main() {
	totals := make(map[string]int)
	sessions := []Session{
		{Title: "Poems", Minutes: 10},
		{Title: "Essays", Minutes: 20},
		{Title: "Poems", Minutes: 15},
	}
	for _, s := range sessions {
		totals[s.Title] += s.Minutes
	}
	fmt.Println(totals["Poems"])
	fmt.Println(totals["Essays"])
}
Text
25
20

Poems appears twice, so its total collects both durations. Printing by explicit key keeps the output in the same order on every run.

Before Day 5

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

  • Declare a struct type with two fields and build a value with named fields.
  • Change the third record in a slice of structs, and explain why changing the range variable would not work.
  • Tell a missing map key from a key that holds 0.

Day 5 connects your program to the outside world. You will read lines a user types, split them into fields and turn text into numbers, checking each step for errors.

Was this article helpful?

Your response is saved on this device.