如何减少 Golang 中重复函数的冗余代码?

我有一个 Rest API 应用程序可以将所有 json 数据列出到浏览器。只要我有更多模块,我的代码就会更加冗余。和复杂。


func UserList(w http.ResponseWriter, r *http.Request) {

    list := []models.User{}

    db.Find(&list)

    json.NewEncoder(w).Encode(list)

}


func ProductList(w http.ResponseWriter, r *http.Request) {

    list := []models.Product{}

    db.Find(&list)

    json.NewEncoder(w).Encode(list)

}


func OrderList(w http.ResponseWriter, r *http.Request) {

    list := []models.Order{}

    db.Find(&list)

    json.NewEncoder(w).Encode(list)

}

有没有更好的解决方案可以将这段代码变成一个函数示例


func List(w http.ResponseWriter, r *http.Request) {

    list := ??? List of struct here ???

    db.Find(&list)

    json.NewEncoder(w).Encode(list)

}


MYYA
浏览 217回答 3
3回答

繁星coding

你可以这样做:func List(list interface{}, w http.ResponseWriter, r *http.Request,) {    db.Find(list)    json.NewEncoder(w).Encode(list)}

饮歌长啸

鉴于您正在调用,db.Find(&list)我假设它们共享一个通用接口。在这种情况下,您可以像这样包装您的处理程序调用;func ListHandler(list <YOUR_INTERFACE>) func(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; return func(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; &nbsp; &nbsp; db.Find(&list)&nbsp; &nbsp; &nbsp; &nbsp; json.NewEncoder(w).Encode(list)&nbsp; &nbsp; }}在您的电话中;http.HandleFunc("/user/list", ListHandler([]models.User{}))http.HandleFunc("/product/list", ListHandler([]models.Product{}))http.HandleFunc("/order/list", ListHandler([]models.Order{}))

呼如林

如果您将模型类型作为请求参数传递,则应该这样做(包括错误处理):func List(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; var list interface{}&nbsp; &nbsp; switch r.FormValue("model") {&nbsp; &nbsp; case "user":&nbsp; &nbsp; &nbsp; &nbsp; list = new([]models.User)&nbsp; &nbsp; case "product":&nbsp; &nbsp; &nbsp; &nbsp; list = new([]models.Product)&nbsp; &nbsp; case "order":&nbsp; &nbsp; &nbsp; &nbsp; list = new([]models.Order)&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; http.Error(w, "invalid type", http.StatusBadRequest)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; if err := db.Find(list); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; http.Error(w, "db error", http.StatusInternalServerError)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; if err := json.NewEncoder(w).Encode(list); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Printf("json encoding error: %v", err)&nbsp; &nbsp; }}另一种选择是建立一个类型的注册表,甚至切片的创建可以被分解,使用帮助reflect:var reg = map[string]reflect.Type{&nbsp; &nbsp; "user":&nbsp; &nbsp; reflect.TypeOf((*models.User)(nil)).Elem(),&nbsp; &nbsp; "product": reflect.TypeOf((*models.Product)(nil)).Elem(),&nbsp; &nbsp; "order":&nbsp; &nbsp;reflect.TypeOf((*models.Order)(nil)).Elem(),}func List(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; etype := reg[r.FormValue("model")]&nbsp; &nbsp; if etype == nil {&nbsp; &nbsp; &nbsp; &nbsp; http.Error(w, "invalid type", http.StatusBadRequest)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; list := reflect.New(reflect.SliceOf(etype)).Interface()&nbsp; &nbsp; if err := db.Find(list); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; http.Error(w, "db error", http.StatusInternalServerError)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; if err := json.NewEncoder(w).Encode(list); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Printf("json encoding error: %v", err)&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go