Nulik
Nulik

Reputation: 7350

What do double curly brackets mean in struct declaration?

Can someone please explain me what do double curly brackets {{ mean in this code ? :

func (t *testService) APIs() []rpc.API {
    return []rpc.API{{
        Namespace: "test",
        Version:   "1.0",
        Service: &TestAPI{
            state:     &t.state,
            peerCount: &t.peerCount,
        },
    }}
}

AFIK single curly bracket is enough to create a struct, so why to double it?

the API struct is defined like this:

package rpc

// API describes the set of methods offered over the RPC interface
type API struct {
    Namespace string      // namespace under which the rpc methods of Service are exposed
    Version   string      // api version for DApp's
    Service   interface{} // receiver instance which holds the methods
    Public    bool        // indication if the methods must be considered safe for public use
}

Upvotes: 0

Views: 1441

Answers (2)

Andy Schweig
Andy Schweig

Reputation: 6749

It's a slightly shorter version of this (fewer lines and less indenting):

return []rpc.API{
    {
        Namespace: "test",
        Version:   "1.0",
        Service: &TestAPI{
            state:     &t.state,
            peerCount: &t.peerCount,
        },
    },
}

It's a slice of structs with one element in it.

Upvotes: 4

Luke Joshua Park
Luke Joshua Park

Reputation: 9795

[]rpc.API{ } defines an empty slice of rpc.API's. You can put any number of rpc.API's inside those curly brackets to make them elements of the slice.

The code you have is the same as:

a := rpc.API{ Namespace: "test", ... }
return []rpc.API{ a }

Upvotes: 2

Related Questions