gpt4 book ai didi

function - 如何更改sqlite get函数?

转载 作者:数据小太阳 更新时间:2023-10-29 03:03:00 26 4
gpt4 key购买 nike

如何更改我的 Get 函数,使其只返回一个 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
}

我想使用这个函数在我的 Controller 中填充一个设备阵列。但现在它不起作用,因为我的函数返回 2 个对象(设备和错误)

我想在这里使用它:

    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)
}
}

所以我的第二个问题是,如何将数组作为数据传递到我的购物车函数中

最佳答案

在 Go 中,永远不要忽略错误。例如,

equipment, err := model.GetEquipment(model.Db)
if err != nil {
// handle error
}
data := CartData{
Name: "Cart",
Equipment: equipment,
// ...
}

关于function - 如何更改sqlite get函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50896725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com