2.1 注意事项
- message 成员编写 编号 不能重复
- message 可以嵌套
- 数组 切片 使用 repeated 关键字
- 联合体使用 oneof 关键字
2.3 内容编写
// 设定标准
syntax = "proto3";
// 指定所在包名
package pb;
// 指定 go 文件所在包名
option go_package ="../micro-1";
enum Week{
Monday = 0;
Tuesday = 1;
}
// 消息体
message Student{
int32 age = 1;
string name = 2;
repeated int32 score = 4;
Week w = 5;
// 联合体
oneof data{
string teacher = 6;
string class = 7;
}
}
// 消息体可以嵌套
message People{
int32 weight = 1;
}
2.3 编译
protoc --go_out=./ *.proto
2.4 添加grpc服务
首先安装grpc插件
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
// 设定标准
syntax = "proto3";
// 指定所在包名
package pb;
// 指定 go 文件所在包名
option go_package ="../micro-1";
enum Week{
Monday = 0;
Tuesday = 1;
}
// 消息体
message Student{
int32 age = 1;
string name = 2;
repeated int32 score = 4;
Week w = 5;
// 联合体
oneof data{
string teacher = 6;
string class = 7;
}
}
// 消息体可以嵌套
message People{
int32 weight = 1;
}
service hello{
rpc HelloWorld(People) returns(Student);
}
编译命令
protoc --go-grpc_out=./ *.proto