Golang括号语法解释

标签 go

我不知道这段代码中这些括号的作用是什么:

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/

相关文章:

json - 时间 JSON 编码为 0 时间

go - 在没有调试信息的情况下构建 Go 二进制文件

pointers - 尽管映射始终是引用类型,但如果它们是从非指针接收器返回的呢?

go - 如何在 for 循环中创建持续时间 -(不匹配类型 int 和 time.Duration)

if-statement - 在模板中使用条件

http - 从 http 响应主体读取 golang

go - os.Args 只读取在 golang 中运行文件时给出的一些数据

GO lang NATS 队列与多个队列订阅

bash - 如何通过golang获取环境变量PS1?

arrays - 在 GoLang 中将字节 slice "[]uint8"转换为 float64