package main import ( "encoding/json" "net/http" "strconv" ) type Product struct { ID int `json:"id"` Name string `json:"name"` Price float64 `json:"price"` Discount float64 `json:"discount"` } var products = []Product{ {1, "Notebook", 4.50, 0}, {2, "Pen", 1.20, 0.1}, {3, "Backpack", 39.00, 0.25}, } func listProducts(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(products) } func getProduct(w http.ResponseWriter, r *http.Request) { id, _ := strconv.Atoi(r.PathValue("id")) for _, p := range products { if p.ID == id { json.NewEncoder(w).Encode(p) return } } }