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 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集