Reading — step 1 of 5
Read
~1 min readCode Generation
Code Generation
protoc reads .proto files and generates language-specific code.
protoc --python_out=. --go_out=. --java_out=. user.proto
Generated Python:
python
Generated Go:
go
The generated code is large but fast: hand-written serialization for known schemas.
Plugins:
protoc-gen-go: standard Go.protoc-gen-go-grpc: Go gRPC stubs.protoc-gen-doc: documentation.protoc-gen-validate: runtime validation.protoc-gen-openapi: REST/OpenAPI from proto.
Modern tools:
- buf: protobuf compiler + linter + formatter, faster than protoc.
- buf.gen.yaml: declarative generation config.
- Connect-RPC: REST-friendly gRPC alternative.
Build integration:
- Bazel: protoc rules.
- gradle/Maven: protoc plugin.
- Make/CMake: custom rules.
- Pre-commit: regenerate on proto change.
Reflection:
- protobuf can describe itself (FileDescriptor).
- Used by gRPC reflection (reflection.proto).
grpcurlqueries gRPC server's schema dynamically.
JSON mapping:
- Each proto type has canonical JSON form.
- For interop with non-proto consumers.
- google.protobuf.json_format.MessageToJson / Parse.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…