liveBook · Manning

Protoc

As explained in gRPC Microservices in Go

protoc is the Protocol Buffers compiler that translates schema definitions into generated source code and related artifacts.

protoc is the Protocol Buffers compiler. It turns .proto message and service definitions into language-specific code for gRPC clients and servers. In an order-creation flow, generated Payment stubs let the Order service call Payment without writing the client interface or data-exchange code by hand; each service still implements its own business logic. section 2.5.1 section 3.2

To generate Go code:

  • Define request and response messages and an RPC service in a .proto file. Set go_package for the generated package. section 3.2.2
  • Install protoc, then install protoc-gen-go and protoc-gen-go-grpc with go install. section 3.2.1
  • Run protoc with Go message and gRPC output options. Use -I when imported .proto files need a search path; paths=source_relative preserves the input-relative folder structure. section 3.2.2
Listing 2.1 Command to generate source code for Go
protoc \
    --go_out=. \
    --go_opt=paths=source_relative \
    --go-grpc_out=. \
    --go-grpc_opt=paths=source_relative \
    payment.proto

The Payment command generates Go message code and gRPC code beside payment.proto using source-relative paths. section 2.5.2

For proto/order.proto, generation produces order.pb.go for message operations and order_grpc.pb.go for gRPC functionality. The latter exposes the generated NewOrderClient constructor and OrderClient interface. Import the generated package, create a client and call Create; implement the server-side behavior separately. section 3.2.2 section 3.2

Order calls Payment

The Order service dials Payment, passes the connection to pb.NewPaymentClient, then calls payment.Create(ctx, request). The generated method accepts a context even though the .proto RPC declaration does not specify one. A timeout can cancel a call that receives no response; the caller must still handle errors and decide when to retry. section 2.5.3

Generated code can stay with a service or be published from a separate schema repository. A GitHub Actions workflow can run protoc for Order, Payment and Shipping when a version tag is pushed, initialize Go modules and publish generated artifacts for consumers. Keep schema changes backward and forward compatible to avoid service disruption or data loss. section 3.3.2 section 3.5

How gRPC Microservices in Go covers it

How deep it goes
Details installation, generation flags, output files, client use and CI automation; leaves service business logic to implementers.
Examples
Uses Payment and Order RPC definitions, protoc commands, generated Go clients and a tag-triggered workflow for three services.
What it assumes
Starts with a .proto service definition and shows how to install protoc and its two Go generation plugins.
What you can do after
Apply it via protoc to generate Go clients and servers, then automate their publication with GitHub Actions.
How it connects
Connects Protocol Buffers schemas to gRPC stubs, Go modules, GitHub Actions and schema compatibility.

Also explained in

Compare how 4 books explain protoc →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of gRPC Microservices in Go. The section links point to the parts of the book it draws on.