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条)

报告相同问题?

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录