安装
go get -d -u github.com/golang/protobuf/protoc-gen-go
go install github.com/golang/protobuf/protoc-gen-go
定义proto文件
syntax = "proto3";
package model;
message SayParam {
string msg = 1;
}
message Pair {
int32 key = 1;
string values = 2;
}
message SayResponse {
string msg = 1;
// 数组
repeated string values = 2;
// map
map<string, Pair> header = 3;
RespType type = 4;
}
enum RespType {
NONE = 0;
ASCEND = 1;
DESCEND = 2;
}
// 服务接口
service Say {
rpc Hello(SayParam) returns (SayResponse) {}
}
生成go代码
protoc --proto_path=/home/www/go/src --go_out=. /home/www/go/src/protoTest/common.proto
- 其中的
proto_path
参数根据手册描述:
--proto_path=PATH Specify the directory in which to search for
imports. May be specified multiple times;
directories will be searched in order. If not
given, the current working directory is used.
If not found in any of the these directories,
the --descriptor_set_in descriptors will be
checked for required proto file.
- 表示导入包的寻找路径
- 命令的最后参数是proto文件路径
参考资料
有疑问加站长微信联系(非本文作者)