Product Catalog Lab
In this lab you will build a product catalog serializer that marshals structured Go data to JSON, unmarshals it back, and filters products by price.
Data Types
InStock uses omitempty so products that are out of stock (false) are omitted from the JSON output, keeping the payload lean.
Steps
1. Create a catalog
Populate a Catalog with three products:
| ID | Name | Price | InStock |
|---|---|---|---|
| p1 | Widget | 9.99 | true |
| p2 | Gadget | 24.99 | true |
| p3 | Doohickey | 4.99 | false |
Set Version to "1.0".
2. Marshal to indented JSON and print
Use json.MarshalIndent with two-space indentation. Convert the result to a string and print it. The output should contain the "products" key.
3. Unmarshal back and print count
Unmarshal the JSON bytes back into a new Catalog value. Print the number of products using fmt.Printf.
4. Implement FindByPrice
Return all products whose Price is less than or equal to maxPrice. The function must be pure โ no side effects, no modifications to the input catalog.
Call FindByPrice with a maxPrice of 10.0 and print each matching product's name.