Generated Client

Learn about the client code generated by Goa, including client-side endpoints and the client struct.

Goa Endpoint

The endpoint abstraction represents a single RPC method in your service. An endpoint may be created server side to represent a method that the service implements, or client side to represent a method that the client calls. In both cases, an endpoint can be represented by the goa.Endpoint function.

Endpoint Client

Continuing with our calc example: Goa generates a transport-agnostic client struct in gen/calc/client.go. This struct contains the client-side endpoints and provides typed methods to make service requests. The generated client code looks like this:

// Client is the "calc" service client.
type Client struct {
        MultiplyEndpoint goa.Endpoint
        AddEndpoint      goa.Endpoint
}

// NewClient initializes a "calc" service client given the endpoints.
func NewClient(multiply, add goa.Endpoint) *Client {
        return &Client{
                AddEndpoint:      add,
                MultiplyEndpoint: multiply,
        }
}

// Add calls the "add" endpoint of the "calc" service.
func (c *Client) Add(ctx context.Context, p *AddPayload) (res int, err error) {
        var ires any
        ires, err = c.AddEndpoint(ctx, p)
        if err != nil {
                return
        }
        return ires.(int), nil
}

// Multiply calls the "multiply" endpoint of the "calc" service.
func (c *Client) Multiply(ctx context.Context, p *MultiplyPayload) (res int, err error) {
        var ires any
        ires, err = c.MultiplyEndpoint(ctx, p)
        if err != nil {
                return
        }
        return ires.(int), nil
}

The client struct contains two fields, AddEndpoint and MultiplyEndpoint, which represent the client-side endpoints for the add and multiply methods. The NewClient function initializes the client struct with the provided endpoints.

The transport-specific code generated by Goa includes factory methods that create endpoints for the transport layer. These factory methods are used to initialize the client struct with the appropriate endpoints.

See the HTTP and gRPC sections for more information about the transport-specific client implementations.