Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang godoc - Explain group type declarations

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

like image 834
Andrei Avatar asked Dec 03 '25 21:12

Andrei


1 Answers

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?.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!