Grouping with map[K][]V
Sometimes you don't need a count but a list per group: every item on a shelf, every order from a customer, every neighbour of a point. The shape is a map whose values are slices:
shelves := make(map[string][]string)
shelves["B3"] = append(shelves["B3"], "hammer")
Appending to nil is safe: a missing key reads as a nil slice, and append makes a new one. So there's no need to check whether the key exists yet.
Output you can count on
Printing a map directly gives a random order, and an output exercise compares every character. For the same output on every run:
- collect the keys into a slice and sort it with
slices.Sort; - sort each group's items too if their order matters.
Dropping duplicates from a sorted slice
After sorting, equal values sit next to each other. slices.Compact removes every value that equals the one just before it:
items := []string{"hammer", "chisel", "hammer"}
slices.Sort(items) // [chisel hammer hammer]
items = slices.Compact(items) // [chisel hammer]
Store the result back in the variable: Compact returns a shorter slice. Another way is the set from the previous lesson, then sorting its keys.
Task
A tool store logs what's on its shelves, one item per line: <shelf> <item>. Item names can be several words long, and some items are logged twice. The starter code already groups the items by shelf.
Print every shelf in alphabetical order of the shelf name, one line per shelf:
<shelf>: <item>, <item>, ...
The items on each shelf are in alphabetical order, without duplicates, joined with ", " (strings.Join). The output must match the expected output exactly.