【发布时间】:2020-06-22 11:44:12
【问题描述】:
去这里。试图让 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)
因此,即使我为Order 定义了Bind,它似乎也不会自动将Bind 应用到Orders 的集合/列表中。
谁能看到我错过了什么?一些端点只会返回一个 Order,而其他端点(比如这个)需要能够返回 Orders 的集合/列表。
【问题讨论】:
标签: go compiler-errors go-chi