Introduce external gRPC and Protobuf contracts
Introduce external gRPC and Protobuf contracts
A .proto schema declares messages with numbered fields and service methods. Protobuf binary compatibility depends on field numbers and wire types, not Go struct names. Generate message bindings with protoc-gen-go and RPC stubs with protoc-gen-go-grpc; both require external tools and compatible pinned dependencies. Never reuse removed field numbers. Test old and new clients, including application semantics and JSON gateways.
package main
import ( "fmt")
func compatible(old, next map[int]string) bool { for n, t := range old { if v, ok := next[n]; ok && v != t { return false } } return true}func main() { old := map[int]string{1: "string", 2: "int32"} fmt.Println(compatible(old, map[int]string{1: "string", 2: "int32", 3: "bool"})) fmt.Println(compatible(old, map[int]string{1: "int32"}))}This intentionally conservative schema comparison is a teaching model, not a Protobuf parser or comprehensive compatibility checker. Adding a new unused field can be wire-compatible, but a new required business rule may still break old callers. Deletion requires reserving numbers and preferably names; compare actual serialized messages with generated code before rollout.
Expected output:
truefalseCheck field presence separately from zero values
package main
import ( "fmt")
type Patch struct{ Count *int }
func main() { zero := 0 for _, p := range []Patch{{}, {Count: &zero}} { fmt.Println(p.Count != nil) }}The pointer models absence versus explicit zero. Proto3 optional scalar fields support explicit presence; ordinary implicit-presence scalar fields do not distinguish unset from their default. Actual generated APIs depend on the chosen Protobuf Go API mode. Do not copy this Go struct as a generated message.
Expected output:
falsetrueRead a real service schema
Save as inventory.proto in a matching example.com/inventory module. After installing reviewed compatible versions of protoc and both Go plugins, generate bindings using protoc --go_out=. --go_opt=module=example.com/inventory --go-grpc_out=. --go-grpc_opt=module=example.com/inventory inventory.proto. Implement Get and Watch, register the generated service on a gRPC server, configure TLS/authentication and test a client against it. This schema is documentation-reviewed, not compiled here. Watch is server streaming; field 2 and its old name remain reserved.
syntax = "proto3";package inventory.v1;option go_package = "example.com/inventory/gen/inventoryv1";message GetRequest { string sku = 1; }message Stock { string sku = 1; reserved 2; reserved "legacy_count"; optional int32 available = 3;}service Inventory { rpc Get(GetRequest) returns (Stock); rpc Watch(GetRequest) returns (stream Stock);}Try it locally
Model reserved tags 2 and 5 in a map and reject a proposed tag 2; accept 3. This is only a reservation check, not full schema validation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Service protocols and cloud 02: Introduce external gRPC and Protobuf contracts
Check the lesson objective before moving on.
What should happen to a deleted Protobuf field number?
Can wire-compatible changes break business behavior?