1、安装
- install grpc
go get -u google.golang.org/grpc
- install protocol buffer v3
PS:本文基于Window64安装,下载地址:https://github.com/protocolbuffers/protobuf/releases
将解压后的protoc.exe路径加入PATH环境变量中
- install the protoc plugin for Go
go get -u github.com/golang/protobuf/protoc-gen-go
2、.protoc生成与编译
2.1、.protoc文件解析
2.2、protoc命令解析
protoc --proto_path=IMPORT_PATH --cpp_out=DST_DIR --java_out=DST_DIR --python_out=DST_DIR path/file.proto
其中,protoc_path表示导入文件搜索路径,默认当前,可以简写为-I;--cpp_out...表示生成文件路径;path/file.protoc表示待编译文件。
如examples/helloworld.protoc的go编译命令为:
protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld
扩展阅读:
https://grpc.io/docs/quickstart/go.html