Count open tasks
Add a line after Tasks: in the report. Print Open: followed by the count of unfinished tasks. Keep any write error visible to the caller. Test an empty queue, the sample input, and a queue whose only task is done.
Hint 1: the idea
The report already receives the ordered snapshot. Count unfinished entries from that slice.
Hint 2: a first step
Check the Tasks: line's write error before writing another line.
Worked solution
Replace the last two lines in report with:
if _, err := fmt.Fprintln(w, "Tasks:", len(tasks)); err != nil {
return err
}
open := 0
for _, task := range tasks {
if !task.Done {
open++
}
}
_, err := fmt.Fprintln(w, "Open:", open)
return errThe sample produces Open: 1. An empty or fully completed queue produces Open: 0. The browser playground has no checks.go, but the downloaded project does: "Try it locally" below shows how its test reacts to this change and where to update the expected transcript.
Try it locally
Download the original task queue. Extract it and run the following from the directory containing go.mod with Go 1.24 or newer.
go run . < sample.txt
go test .The browser uses one main package with an alternate entry file for checks. Locally, the Go testing package calls the same check function through queue_test.go. That file is short enough to read in full:
package main
import "testing"
func TestQueueContract(t *testing.T) {
if err := checkQueue(); err != nil {
t.Fatal(err)
}
}go testcompiles every file in the folder, including those ending in_test.go, and runs each function whose name starts withTestand that takes a*testing.T.checkQueuelives inchecks.go. It is the same function the browser check runs. It builds queues, runs transcripts and returns the first broken rule as an error, ornil.t.Fatal(err)marks the test as failed, prints the error and stops the test. If you tried the optional Go test section on Day 6, this is the same idea with one test instead of a table.
Run go test -v . to see the test by name:
=== RUN TestQueueContract
--- PASS: TestQueueContract (0.00s)
PASSNow break a rule on purpose. In queue.go, change the duplicate check if _, exists := q.tasks[id]; exists { to if false { and run go test . again:
--- FAIL: TestQueueContract (0.00s)
queue_test.go:7: duplicate ID: got <nil>
FAILThe message names the broken rule: adding a duplicate ID returned no error. Restore the line. If you keep the Open: change from the exercise above, go test fails in the same way until you update the expected transcript in checks.go.
Completion is saved in this browser only. It does not measure mastery.