gpt4 book ai didi

用于子路径路由的 Gorilla Mux

转载 作者:IT王子 更新时间:2023-10-29 02:34:16 27 4
gpt4 key购买 nike

我有以下路线:

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)

所有处理程序都在另一个包中分离

有什么方法可以在单独的包中匹配这些路径吗?

谢谢,

最佳答案

您可以为您的路由器创建一个包,然后导入该包并添加您的路由。

路由器

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)

//...
}

关于用于子路径路由的 Gorilla Mux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28351063/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com