【发布时间】:2017-08-23 07:15:16
【问题描述】:
我一直在尝试设置一个 JSON 配置文件来为我的应用程序设置动态路由。这个想法是,我将能够根据谁在使用该服务来设置我自己的 URL 结构。我有一个接受 JSON 的结构并且工作正常。我正在使用 gorilla mux。
type CustomRoute struct {
Name string
Method string
Path string
HandleFunc string
}
JSON 与 struct 基本相同,并且可以正常运行。
我遇到的问题是获取 HandleFunc 部分。
代码如下:
func NewRouter() *mux.Router {
routerInstance := mux.NewRouter().StrictSlash(true)
/*
All routes from the routing table
*/
// r = []CustomRoute with the JSON data
r := loadRoute()
for _, route := range r {
var handler http.Handler
handler = route.HandlerFunc
handler = core.Logger(handler, route.Name)
routerInstance.
Methods(route.Method).
Path(route.Path).
Name(route.Name).
Handler(handler)
}
return routerInstance
}
我总是收到以下错误(正如人们所期望的那样)
不能在赋值中使用 route.HandlerFunc (type string) 作为 http.Handler 类型: string 没有实现 http.Handler(缺少 ServeHTTP 方法)
有人告诉我使用类似的东西:
var functions = map[string]interface{}{
"HandleFunc1": HandleFunc1,
}
但我不知道如何完成这项工作
【问题讨论】:
标签: json go routing mux gorilla