我有一个大路由器:
router.HandleFunc("/tags", controllers.GetTags)
router.HandleFunc("/tags/items/{tagId}", controllers.GetTaggedItemsByTagId).Methods("GET")
// ...
每个这样的函数看起来像:
func GetTags(w http.ResponseWriter, r *http.Request) {
tags, err := (&postgres.TagsService{}).GetTags()
if err != nil {
log.Println(err)
w.WriteHeader(500)
} else {
w.Header().Add("Content-Type", "application/json")
resp, _ := json.Marshal(tags)
w.Write(resp)
}
}
func GetTaggedItemsByTagId(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
tagId, err := strconv.Atoi(vars["tagId"])
items, err := (&postgres.TagsService{}).GetTaggedItemsByTagId(tagId)
if err != nil {
log.Println(err)
w.WriteHeader(500)
} else {
w.Header().Add("Content-Type", "application/json")
resp, _ := json.Marshal(items)
w.Write(resp)
}
}
在每个函数中,我从数据库中获取数据,将结果序列化为 json
并将其返回给客户端。
我需要一些东西来避免代码重复。像这样的东西:
func Handler(err error, data object) {
if err != nil {
log.Println(err)
w.WriteHeader(500)
} else {
w.Header().Add("Content-Type", "application/json")
resp, _ := json.Marshal(object)
w.Write(resp)
}
}
我不能这样做,因为 Go 是一种类型化的语言。在这种情况下,Go 中避免代码重复的最佳方法是什么?
最佳答案
为每个 http 处理程序使用函数签名的类型
type HandlerFunc func(w http.ResponseWriter, req *http.Request) (interface{}, error)
然后像这样包装函数
func WrapHandler(h HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
d, err := h(w, req)
// handle errors and sending out the data
}
然后使用
设置处理程序router.Get('/myroute', WrapHandler(GetTaggedItemsByTagId))
关于go - 如何避免路由器处理代码中的代码重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42977404/