dongzeao5047 2016-07-29 15:26
浏览 62
已采纳

Golang Godoc-解释组类型声明

There is a piece of GO code I have seen in the GIN library and in the Google docs that look like the following

type (
    T0 []string
    T1 []string
    T2 struct{ a, b int }
    T3 struct{ a, c int }
    T4 func(int, float64) *T0
    T5 func(x int, y float64) *[]string
)

What I don't understand is, what this grouping is doing and what are some purposes of this implementation (there wasn't much in the docs that went over this topic unless I missed it)

another example from the gin library

type (
    RoutesInfo []RouteInfo
    RouteInfo  struct {
        Method  string
        Path    string
        Handler string
    }
        Engine struct {
        RouterGroup
        HTMLRender  render.HTMLRender
        allNoRoute  HandlersChain
        allNoMethod HandlersChain
        noRoute     HandlersChain
        noMethod    HandlersChain
        pool        sync.Pool
        trees       methodTrees

        RedirectTrailingSlash bool


        RedirectFixedPath bool      
        HandleMethodNotAllowed bool
        ForwardedByClientIP    bool
    }
)

And lastly - sorry this is different topic but related to this one

var _ IRouter = &Engine{}

why is there a _ infront of IRouter? I know it's a blank identifier but what purpose does it have in that case

  • 写回答

3条回答 默认 最新

  • douti9253 2016-07-29 16:08
    关注

    The code

    type (
       A int
       B string
    )
    

    is functionality identical to

    type A int
    type B string
    

    The grouping is just a way to organize code. The grouping is sometimes used to indicate that the types are related in some way.

    The use of the blank identifier is explained in Specs: What's the purpose of the blank identifier in variable assignment?.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 定制ai直播实时换脸软件
  • ¥100 栈回溯相关,模块加载后KiExceptionDispatch无法正常回溯了
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页
  • ¥20 如何通过代码传输视频到亚马逊平台