Generated-code reference

Generated-code reference

This page describes the code generated when compiling .proto files with protoc, using the protoc-gen-go-grpc grpc plugin. This latest version of generated code uses generics by default. If you’re working with older generated code that doesn’t use generics, you can find the relevant documentation here. While we encourage using this latest version with generics, you can temporarily revert to the old behavior by setting the useGenericStreams flag to false.

You can find out how to define a gRPC service in a .proto file in Service definition.

Thread-safety: note that client-side RPC invocations and server-side RPC handlers are thread-safe and are meant to be run on concurrent goroutines. But also note that for individual streams, incoming and outgoing data is bi-directional but serial; so e.g. individual streams do not support concurrent reads or concurrent writes (but reads are safely concurrent with writes).

Methods on generated server interfaces

On the server side, each service Bar in the .proto file results in the function:

func RegisterBarServer(s *grpc.Server, srv BarServer)

The application can define a concrete implementation of the BarServer interface and register it with a grpc.Server instance (before starting the server instance) by using this function.

Unary methods

These methods have the following signature on the generated service interface:

Foo(context.Context, *RequestMsg) (*ResponseMsg, error)

In this context, RequestMsg is the protobuf message sent from the client, and ResponseMsg is the protobuf message sent back from the server.

Server-streaming methods

These methods have the following signature on the generated service interface: Foo(*RequestMsg, grpc.ServerStreamingServer[*ResponseMsg]) error

In this context,