如下方式定义结构体
/*
type Route struct { // the path is "/"
// 空结构体
}
*/
type RouteApple struct { // the path is "/apple/:kind"
nni.Route `url:"apple"`
kind string // 这里的 kind 关联 path param,可以用依赖注入的方式设置值
}
type RouteAppleGet struct { // the path is "/apple/:kind/get/:number"
RouteApple `url:"get"`
number string
nni.GET // 这个 GET 指 http request method。是空结构体
}
func TestExample1(t *testing.T){
fmt.Println(nni.Parse(&RouteAppleGet{}))
}
nni.Parse()
output:
{/apple/:kind/get/:number/ get}
可以是这样
type RouteAppleGet struct { // the path is "/apple/:kind/get/:number"
RouteApple `url:"get"`
number string
nni.GET
}
func (handler *RouteAppleGet) handle(c *gin.Context){
}
router.Registe(&RouteAppleGet{})
你不用像下面这样通过注释的方式弱捆绑 URL 到 handler 上
// @router /***/**/handle1 [POST]
func Handle1(c *gin.Context) {
而且这样从下向上的书写 URL,也很容易写错 parent path /***/**/