我不知道这段代码中这些括号的作用是什么:
func main() {
router := gin.Default()
v1 := router.Group("/v1")
{ // <- these brackets
v1.POST("/login", loginEndpoint)
v1.POST("/submit", submitEndpoint)
v1.POST("/read", readEndpoint)
} // <- and these
v2 := router.Group("/v2")
{ // <- and these
v2.POST("/login", loginEndpoint)
v2.POST("/submit", submitEndpoint)
v2.POST("/read", readEndpoint)
} // <- and these
router.Run(":8080")
}
从这里得到这个代码:https://github.com/gin-gonic/gin#grouping-routes 你能解释一下吗?
最佳答案
由于词法作用域(如评论的那样)没有任何作用域,所以它在这里更像是一种“编码风格”,如 gin-gonic/gin
issue 108 中所表达的那样
I, personally, like to use the brackets because it gives a sense of group, and makes the routes easier to read.
// notify the "test" variable
test := r.Group("/", TestHandle())
{
test.GET("/test", func(c *gin.Context) {
fmt.Printf("test")
c.String(200, "ok")
})
}
关于Golang括号语法解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60137945/