how can I change my Get function so that it just returns one Equipment-Objekt?
func GetEquipmentByID(Id string) (equipment Equipment, err error) {
equipment = Equipment{}
err = Db.QueryRow("select ID, Name, Description, ImgPath, Category, Availability, Amount, Storage from Equipment where Id = $1", Id).Scan(&equipment.ID, &equipment.Name, &equipment.Description, &equipment.ImgPath, &equipment.Category, &equipment.Availability, &equipment.Amount, &equipment.Storage)
return
}
I want to use this function to fill an Equipment Array in my Controller. But now it doesnt work, because my function returns 2 objects (Equipment and Error)
I want to use it here:
func Cart(w http.ResponseWriter, r *http.Request) {
data := CartData{
Name: "Cart",
Equipment: model.GetEquipment(model.Db),
CartList: nil,
Pages: []Page{
{
Title: "Meine Geräte",
Active: false,
Link: "/my-equipment",
},
{
Title: "Equipment",
Active: false,
Link: "/equipment",
},
{
Title: "Logout",
Active: false,
Link: "/logout",
},
},
}
session, _ := store.Get(r, "cookie-name")
list := session.Values["EquipmentIDs"].(string)
result := strings.Split(list, ",")
for i := range result {
fmt.Print(i)
fmt.Println(result[i])
}
for i:= range result {
equipment, err := model.GetEquipmentByID(result[i])
if err != nil {
// handle error
}
data.CartList[i] = equipment // ERROR BECAUSE OF THIS LINE
}
tmpl:= template.Must(template.ParseFiles("template/base_user.html", "template/cart.html"))
tmpl.ExecuteTemplate(w, "base", data)
}
}
So my second question is, how can I pass the array in my cart function as data