如何在protobuf中定义数组



我的服务器需要一个数组输入,比如

[
{
"name":"ada",
"age":"20"
}
]

我不知道该定义protobuf,谢谢你的考虑

数组不是protobuf中的概念;有repeated,但如何解释它是特定于实现的;它可以是一个数组,但不需要是;至于.proto:

syntax = "proto3";
message SomeWrapper {
repeated SomeInner items = 1;
}
message SomeInner {
string name = 1;
int32 age = 2;
}

在我的例子中,我有ListProductapi,它正在返回slice。

产品结构

type Product struct {
ID int32
Name String 
Price int32
Category string
}

ListProduct Resp

[]Product

答案:product.proto文件

syntax = "proto3";
package product;
option go_package = "product/grpc";
message Product {
int32 id = 1;
string name = 2; 
int32 price = 3;
int32 category = 4;

}
message ListProductResponse{ repeated Product products = 1; }

如果您想要地图[int32]产品

message ListProductResponse{ map< int32, Product> products = 1; }

最新更新