douxian8883
2018-12-26 19:05
浏览 66
已采纳

来自gin-gonic文档的语法问题

I have been developing in Node/Python for years, and over the holiday I have been trying to expand my knowledge of Go. I have a pet project that I've been working on to learn it.

As I have been reading the gin-gonic documentation, I found syntax that I can't wrap my head around.

func main() {
    router := gin.Default()

    // Simple group: v1
    v1 := router.Group("/v1")
    {
        v1.POST("/login", loginEndpoint)
        v1.POST("/submit", submitEndpoint)
        v1.POST("/read", readEndpoint)
    }

    // Simple group: v2
    v2 := router.Group("/v2")
    {
        v2.POST("/login", loginEndpoint)
        v2.POST("/submit", submitEndpoint)
        v2.POST("/read", readEndpoint)
    }

    router.Run(":8080")
}

Basically, it looks like the router.Group() method takes in a struct, but I am not quite sure what the mechanics are here... It's not wrapped in parentheses. How is this object being passed in/handled by the Group method?

图片转代码服务由CSDN问答提供 功能建议

我已经在Node / Python中开发了多年,并且在假期中,我一直在尝试扩展对 走。 我有一个宠物项目,我一直在努力学习。

我一直在阅读 gin-gonic 文档,我发现语法无法缠住。

  func main(){
 router:= gin.Default()
  
 //简单组:v1 
 v1:= router.Group(“ / v1”)
 {
 v1.POST(“ / login”,loginEndpoint)
 v1.POST(“ / submit”,SubmitEndpoint  )
 v1.POST(“ / read”,readEndpoint)
} 
 
 //简单组:v2 
 v2:= router.Group(“ / v2”)
 {
 v2.POST(  “ / login”,loginEndpoint)
 v2.POST(“ / submit”,SubmitEndpoint)
 v2.POST(“ / read”,readEndpoint)
} 
 
 router.Run(“:8080”)\  n} 
   
 
 

基本上,它看起来像 router.Group()方法采用了一个结构,但是我不太确定 机制就在这里...不用括号括起来。 该对象如何通过 Group 方法传入/处理?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhiyan5693 2018-12-26 19:23
    已采纳

    The Go Programming Language Specification

    Blocks

    A block is a possibly empty sequence of declarations and statements within matching brace brackets.

    Declarations and scope

    Go is lexically scoped using blocks.


    For example, the v1.POST method call statements form a block:

    // Simple group: v1
    v1 := router.Group("/v1")
    {
        v1.POST("/login", loginEndpoint)
        v1.POST("/submit", submitEndpoint)
        v1.POST("/read", readEndpoint)
    }
    

    This is an unusual use of blocks.

    打赏 评论

相关推荐 更多相似问题