获取预期的顶级语句(例如 "message" ) 尝试在 Protobuf 中创建 2D 数组时



我正在尝试在.proto文件中创建一个2d数组,如下所示:

message Foo {
repeated int32 items = 1;
}
repeated Foo items= 1;

但是在生成.pb.go文件时,得到该行repeated Foo items= 1;的错误

Getting Expected top-level statement (e.g. "message")

有人遇到过这个错误吗?

请告诉我我们如何解决这个问题?

不能在顶层有字段,所有内容都应该包含在消息或枚举中。所以在你的情况下,你应该有这样的东西:

message Foo {
repeated int32 items = 1;
}
message Bar {
repeated Foo items = 1;
}

然后,您可以通过以下操作设置项目:

&pb.Bar {
Items: []*pb.Foo {
{ Items: []int32{1, 2, 3, 4, 5, 6} },
{ Items: []int32{7, 8, 9, 10, 11, 12} },
},
}

其中pb是您在proto文件中定义的包的导入名称。例如:

原型:

option go_package = "example.com/m/proto";

go:

import pb "example.com/m/proto"

最新更新