protoc-gen-go-restate
which you can install using
go install github.com/restatedev/sdk-go/protoc-gen-go-restate@latest
, in addition to protoc
which you can install from the Protobuf compiler repo.
Defining a service
To get started, create a Protocol Buffer definition file defining a service:service.proto
service.pb.go
containing Go types, and service_restate.pb.go
which contains several useful objects:
NewGreeterClient
, which returns an interface for making type-safe calls to the Greeter service. The default serialization for request and response data is the canonical Protobuf JSON encoding, but this is configurable.GreeterServer
, an interface for server code to implement. An implementor can be converted into a service definition withNewGreeterServer
.UnimplementedGreeterServer
, an empty struct that implementsGreeterServer
by just returning ‘not implemented’ terminal errors for each method. You can choose to embed this struct into your own implementation struct, in which case new methods in the Protobuf definition will not cause compilation errors in your Go code, which can help with backwards compatibility.
Both
NewGreeterClient
and NewGreeterServer
accept option parameters which can be used to set a different serialization codec.
For example, you can use Protobuf by providing restate.WithProto
to both functions.Implementing the server
Next, you can implement theGreeterServer
interface and bind it to your Restate server.
Using the client
NewGreeterClient
will return a similar client to what you’d get from restate.Service
, with additional
type safety for method names and request/response types.
Defining Virtual Objects and Workflows
To define a Virtual Object or Workflow, you will need to provide the optiondev.restate.sdk.go.service_type
when defining the service.
This option is defined by the Go SDK’s proto file
dev/restate/sdk/go.proto
which must be imported in your own file,
and a directory containing it must be provided to protoc eg with -I $GOPATH/src/github.com/restatedev/sdk-go/proto
You may additionally provide the option dev.restate.sdk.go.handler_type
to denote that the handler is a shared handler.
service.proto
Easier Protobuf with Buf
Easier Protobuf with Buf
Instead of manually wrangling And to allow the import of Then you can run
protoc
to handle generation and proto imports,
it can be a lot easier to use Buf.Buf determines what code to generate based on a buf.gen.yaml
file:dev/restate/sdk/go.proto
, you can specify the dependency in your buf.yaml
:buf generate
to generate the code.