A check needs something to compare. For a single number that is easy. For a whole ledger it is not: checking every map entry by hand is long, and it is easy to forget one. A snapshot turns the whole state into one value, here a JSON string, that a check compares in one step. For that to work, the encoding must be canonical: two equal states must always produce exactly the same bytes, whatever order things were added in.
encoding/json is canonical for the types the ledger uses. Struct fields come out in the order they are declared, and map keys come out sorted. That is why the project's report command can print JSON that the checks compare as a plain string.
Predict and run
Two ledgers receive the same stock in different orders. Are their snapshots equal?
package main
import (
"encoding/json"
"fmt"
)
type Snapshot struct {
Stock map[string]int `json:"stock"`
Orders map[string]string `json:"orders"`
}
func main() {
first := Snapshot{Stock: map[string]int{}, Orders: map[string]string{"A1": "open"}}
first.Stock["tent"] = 3
first.Stock["drill"] = 1
second := Snapshot{Stock: map[string]int{"drill": 1, "tent": 3}, Orders: map[string]string{"A1": "open"}}
a, _ := json.Marshal(first)
b, _ := json.Marshal(second)
fmt.Println(string(a))
fmt.Println(string(a) == string(b))
}Check your prediction
{"stock":{"drill":1,"tent":3},"orders":{"A1":"open"}}
trueThe tags such as json:"stock" set the names in the output; without them the keys would be Stock and Orders. drill comes before tent because the encoder sorts map keys. Swap the two fields in the struct declaration and run again: the field order in the JSON follows the declaration, so orders now comes first.
Only exported fields are encoded
encoding/json can only see fields whose names start with a capital letter. A lowercase field is silently left out, which makes a snapshot look complete while hiding part of the state. If a check compares snapshots, a missing field means the check cannot see a bug in it.
package main
import (
"encoding/json"
"fmt"
)
type Order struct {
ID string `json:"id"`
Lines int `json:"lines"`
cancelled bool
}
func main() {
open, _ := json.Marshal(Order{ID: "A1", Lines: 2})
gone, _ := json.Marshal(Order{ID: "A1", Lines: 2, cancelled: true})
fmt.Println(string(open))
fmt.Println(string(gone))
fmt.Println(string(open) == string(gone))
}{"id":"A1","lines":2}
{"id":"A1","lines":2}
trueA cancelled order and an open one look identical. go vet does not catch this, because unexported fields are allowed. The project avoids the problem by building a separate Snapshot value with only exported fields, and its Snapshot method turns each order's cancelled flag into the string "open" or "cancelled".
What makes a snapshot canonical
The ledger's snapshot holds only strings and integers in structs and maps, so its encoding is fully determined. Some values need care. Floating-point results can differ in the last digit depending on how they were computed. Times include a time zone and nanoseconds. Slices keep their order, so a slice built from a map must be sorted first. When a snapshot is meant for checks, keep it to values whose text form is exact, and normalize anything else before encoding it.
Print both sides on failure
When a snapshot comparison fails, print the expected and the actual JSON, one above the other. The project's checks do this with %s, so the difference is visible at a glance instead of buried in a "values differ" message.
Practice
Your turn
Define a BookingSnapshot with a seats map from seat number to booking ID and an open count, using JSON tags. Build one from seats 4 and 2 for B1 and seat 7 for B2, added in that order, and another from the same bookings added in reverse. Print the JSON and whether the two are equal.
The output should be:
{"seats":{"2":"B1","4":"B1","7":"B2"},"open":2}
truepackage main
func main() {
// Define BookingSnapshot, build two and compare their JSON.
}Hint 1: the idea
Map keys are sorted even when they are integers: encoding/json writes them as strings, ordered by number.
Hint 2: a first step
Declare Seats map[int]string with the tag json:"seats" and Open int with json:"open", then fill one map in each order.
Worked solution
package main
import (
"encoding/json"
"fmt"
)
type BookingSnapshot struct {
Seats map[int]string `json:"seats"`
Open int `json:"open"`
}
func main() {
forward := BookingSnapshot{Seats: map[int]string{}, Open: 2}
forward.Seats[4] = "B1"
forward.Seats[2] = "B1"
forward.Seats[7] = "B2"
reverse := BookingSnapshot{Seats: map[int]string{}, Open: 2}
reverse.Seats[7] = "B2"
reverse.Seats[2] = "B1"
reverse.Seats[4] = "B1"
a, err := json.Marshal(forward)
if err != nil {
panic(err)
}
b, err := json.Marshal(reverse)
if err != nil {
panic(err)
}
fmt.Println(string(a))
fmt.Println(string(a) == string(b))
}{"seats":{"2":"B1","4":"B1","7":"B2"},"open":2}
trueThe integer keys became strings in the JSON and are still in numeric order, so the two snapshots are byte for byte the same. A check can now compare a whole register with one string, as the project's checkCancel does.
Completion is saved in this browser only. It does not measure mastery.