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.
Sessionhas aTitlefield of typestringand aMinutesfield of typeint. Declare it outsidemain. - 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.Minutesworks the first time a title appears, because the missing key reads as 0. - Explicit lookups for output. Asking for
totals["Poems"]and thentotals["Essays"]fixes the order yourself.
Plan before you type
- Declare the
Sessiontype with its two fields. - In
main, build the slice with the three records in the order the task gives. - Make an empty map from titles to minutes.
- Range over the slice and add each record's minutes to the entry for its title.
- 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 withvarbut never created withmake. - 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
rangeloop over the map instead of two explicit lookups. - The compiler reports
undefined: Session: the type declaration is missing, or it sits insidemainbelow the line that first uses it.
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.
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
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"])
}25
20Poems 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
rangevariable 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.
Completion is saved in this browser only. It does not measure mastery.