go 的 protoc 插件調用邏輯


要讓protoc使用插件,需要做下面事情:

Place the plugin binary somewhere in the PATH and give it the name "protoc-gen-NAME" (replacing "NAME" with the name of your plugin).

If you then invoke protoc with the parameter –NAME_out=OUT_DIR (again, replace "NAME" with your plugin's name), protoc will invoke your plugin to generate the output, which will be placed in OUT_DIR.

Place the plugin binary anywhere, with any name, and pass the –plugin parameter to protoc to direct it to your plugin like so:

protoc --plugin=protoc-gen-NAME=path/to/mybinary --NAME_out=OUT_DIR

On Windows, make sure to include the .exe suffix:

protoc --plugin=protoc-gen-NAME=path/to/mybinary.exe --NAME_out=OUT_DIR

參考: https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.compiler.plugin

 

產生go的 grpc 代碼的插件命令為:

protoc --go_out=plugins=grpc,import_path=mypackage:. *.proto
 
 
這里通過 --NAME_out ,就能知道是需要找 protoc-gen-NAME 插件, 即 protoc-gen-go 插件。
而 --go_out=plugins=grpc 則會在 https://github.com/golang/protobuf 這里找到 
https://github.com/golang/protobuf/blob/master/protoc-gen-go/grpc/grpc.go  做為插件。
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM