我有下列路线:
router.Methods("POST").Path("/my_post_01").HandlerFunc(myHandler1)
router.Methods("GET").Path("/my_get_01").HandlerFunc(myHandler2)
router.Methods("POST").Path("/my_post_02").HandlerFunc(myHandler3)
router.Methods("GET").Path("/my_get_02").HandlerFunc(myHandler4)
router.Methods("POST").Path("/my_post_03").HandlerFunc(myHandler5)
router.Methods("GET").Path("/my_get_03").HandlerFunc(myHandler6)
router.Methods("POST").Path("/my_post_04").HandlerFunc(myHandler7)
router.Methods("GET").Path("/my_get_04").HandlerFunc(myHandler8)
router.Methods("POST").Path("/my_post_05").HandlerFunc(myHandler9)
router.Methods("GET").Path("/my_get_05").HandlerFunc(myHandler10)由于我有越来越多的路线,它变得更难管理。
我想做这样的事情:
router.Path("/my/*").HandleFunc(mypackage.RegisterHandler)将所有处理程序分隔到另一个包中。
有没有办法在一个单独的包中匹配这些路径?
谢谢,
发布于 2015-02-05 21:36:49
您可以为您的路由器创建一个包,然后导入所述包并添加路由。
路由器
package router
var Router = mux.NewRouter()
// handle "/my/" routes
var MyRouter = Router.PathPrefix("/my").Subrouter()另一个包
import "/path/to/router"
func init() {
router.MyRouter.Methods("POST").Path("/post_01").HandlerFunc(myHandler1)
}主要
import "/path/to/router"
func main() {
http.Handle("/", router.Router)
//...
}https://stackoverflow.com/questions/28351063
复制相似问题