preface
What can the Protoc tool do?
The Protoc tool can compile.proto files into C, C++, Golang, Java, Python, PHP, etc.
This article focuses on generating Golang code from protoc, such as common commands:
protoc -I . --go_out=xxx
Copy the code
For more parameters, run protoc –help to see.
confusion
How do I know which plugin protoc uses?
For example: — what plugin does go_out use? It turns out that the protoc-gen-go plugin is used.
For example: –go-grpc_out which plugin is used? The protoc-gen-go-GRPC plugin is used.
Through the use of other plug-ins, a rule has been concluded:
go_out
The correspondingprotoc-gen-go
Plug-in;go-grpc_out
The correspondingprotoc-gen-go-grpc
Plug-in;- .
*_out
The correspondingprotoc-gen-*
Plug-in;
2. For example, what if the new and old projects use different versions of the Protoc-gen-go plugin?
I can think of two solutions:
- Do it in two environments, for example, play two
docker
Environment, where new projects are generated in one environment and old projects are generated in another. - Do this by differentiating plug-in names, for example, naming the new version as
protoc-gen-go-new
, name the old version asprotoc-gen-go-old
Is used when building a new version--go-new_out
Used when building older versions--go-old_out
.
Obviously, the second option costs less.
What’s the difference between protoc-gen-go and protoc-gen-go-grPC?
When generated with the argument –go_out=plugins= GRPC: XXX, the resulting file *.pb.go contains the message serialization code and the GRPC code.
When generated with the argument –go_out= XXX –go-grpc_out= XXX, two files *.pb.go and *._grpc.pb.go are generated, which are the message serialization code and the gRPC code, respectively.
Why do these two forms exist? How are they different? Here’s what I found:
[protoc]
原文 : Differences between protoc-gen-go and protoc-gen-go-grpc
Is there a recommended version number for protoc and protoc-gen-xxx plug-ins and GRPC and Protobuf when choosing which version to combine?
For example, the combination version number is:
protoc
v3.18.1protoc-gen-go
v1.27.1protoc-gen-go-grpc
v1.1.0grpc
v1.41.0protobuf
v1.27.1
For the above version numbers, is there an official document recommending the version combination? Do any of your friends know? Welcome to leave a comment
The plug-in
- Protoc -gen-validate
- Parameter verification: Go-proto-validators
- Document generation: protoc-gen-doc
- grpc-gateway
- protoc-gen-grpc-gateway
- protoc-gen-openapiv2
What other useful plug-ins have you used? Welcome self-recommendation and recommendation, message area discussion ~
summary
The above are my doubts and corresponding solutions. I hope they can be helpful to you.
Recommended reading
- Go – A little thought about.proto files
- Go – Use sync.waitGroup for concurrent operations
- Go – Use sync.Map to solve the problem of Map concurrency security
- Go – Improves application performance based on escape analysis
- Go – Use sync.Pool to reduce GC stress