到这里。试图让 chi 渲染器返回一个Order结构实例列表并得到一个我不明白的编译器错误:
package myapp
import (
"net/http"
"github.com/go-chi/render"
)
type Order struct {
OrderId string
Status string
}
func (*Order) Bind(r *http.Request) error {
return nil
}
func GetAllOrderByCustomerId(dbClient DbClient, customerId string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// fetch all customer orders from the DB
orders,err := dbClient.FetchAllOrdersByCustomerId(customerId)
if err != nil {
log.Error("unable to fetch orders for customer", err)
render.Render(w, r, NewInternalServerError(err))
return
}
render.Bind(r, &orders)
return
}
}
当我去编译这段代码时,我得到:
fizz/buzz/myapp/order_fetcher.go:136:20: cannot use &orders (type *[]Order) as type render.Binder in argument to render.Bind:
*[]Order does not implement render.Binder (missing Bind method)
因此,即使我定义了Bindfor Order,它似乎也不会自动将其Bind应用于Orders.
谁能看到我错过了什么?一些端点只会返回一个Order,而其他端点(比如这个)需要能够返回一个集合/列表Orders。
一只萌萌小番薯
相关分类