This checkpoint asks for two things at once: a small function that guards a running total, and a table of checks that proves it behaves at its edges. The function is the kind of rule a program like the Day 7 reading log needs before it adds a session to its totals. A bad duration must never change the total, and when a duration is rejected the caller must be told why. The table is how you, and anyone who changes the function later, know that is true.
What you will use
- A function with two results. It takes the current total and a duration, and returns
(int, error): the resulting total andnil, or the original total and an error that explains the rule. - An early return for invalid input. When the duration is outside 1 to 240, return the original total and an error made with
fmt.Errorfbefore any adding happens. - A table of cases. An anonymous struct with the input duration, the expected total and whether an error is expected.
- One comparison loop. Call the function for each row, compare both results, and
panicwith a helpful message on any mismatch.
Plan before you type
- Write the function's signature, and decide what it returns in each of the two situations.
- Write the invalid branch first, then the valid one.
- List the cases. Use the same starting total, such as 10, in every row so the expected totals are easy to work out by hand.
- Choose the four durations that sit on and just outside each boundary, and fill in the expected results.
- Loop over the rows, call the function, compare, and print a summary once everything passes.
Prove the table can fail
When your program prints its success line, break the function on purpose. Change > 240 to >= 240, or forget to return the original total for a rejected duration. Each change should make a row fail. If a deliberate mistake still passes, the table is missing a case.
Mistakes worth checking for
- A rejected duration still changes the total: the function adds before it checks, or returns
total + minuteson the invalid path. - The table passes even with a broken boundary: the rows test 120 and 300 but not 240 and 241.
- Only the total is compared: a function that returns the right total but no error for 241 would slip through.
- The compiler reports
not enough return values: onereturngives only the total and leaves out the error.
Your turn
Write a function add that takes a total and a duration and returns (int, error). For 1 through 240 minutes, return the updated total and nil. For anything else, return the original total and an error such as 241 is outside 1 to 240 minutes. Check both boundaries and the values just outside them with a table, starting every row from a total of 10. When every row passes, print:
4 checks passedpackage main
func main() {
// Build your solution here.
}Hint 1: the idea
Decide whether the duration is valid before adding anything. A row of the table needs the input, the expected total and whether an error is expected.
Hint 2: a first step
Write func add(total, minutes int) (int, error) with the invalid branch first, then call add(10, 241) and print both results. You should see 10 and the message.
Worked solution
package main
import (
"fmt"
)
func add(total, minutes int) (int, error) {
if minutes < 1 || minutes > 240 {
return total, fmt.Errorf("%d is outside 1 to 240 minutes", minutes)
}
return total + minutes, nil
}
func main() {
cases := []struct {
input, want int
wantErr bool
}{
{0, 10, true}, {1, 11, false}, {240, 250, false}, {241, 10, true},
}
for _, c := range cases {
got, err := add(10, c.input)
if got != c.want || (err != nil) != c.wantErr {
panic(fmt.Sprintf("add(10, %d) = %d, %v", c.input, got, err))
}
}
fmt.Printf("%d checks passed\n", len(cases))
}4 checks passedThe four rows sit on and just outside each edge: 0 and 241 fail, 1 and 240 pass. (err != nil) != c.wantErr compares whether an error came back with whether one was expected. A rejected duration returns the original total unchanged, so the caller's total is safe even if it ignores the error by mistake.
Before Day 7
Check that you can do each of these without looking back:
- Write a function that returns a value and an
error, with the zero value on the failure path. - Explain when to
panicand when to return anerror. - Choose the four boundary rows for a rule such as "from 5 to 60".
Day 7 brings everything together in the reading log: a program split across several files that reads sessions from standard input, validates each line, and prints a report, with checks that protect its rules.
Completion is saved in this browser only. It does not measure mastery.