简体   繁体   English

尝试在 Protobuf 中创建二维数组时获取预期的顶级语句(例如“消息”)

[英]Getting Expected top-level statement (e.g. "message") when trying to create 2D array in Protobuf

I am using trying to create a 2d array in.proto file like this:我正在尝试创建一个二维数组 in.proto 文件,如下所示:

message Foo {
    repeated int32 items = 1;
}

repeated Foo items= 1;

but on generating the.pb.go file, getting the error for this line repeated Foo items= 1;但是在生成 .pb.go 文件时,出现此行的错误repeated Foo items= 1;

Getting Expected top-level statement (eg "message")

Has anyone ever came across this error?有没有人遇到过这个错误?

Please let me know how could we resolve this issue?请让我知道我们如何解决这个问题?

You cannot have a field at the top level, everything should be enclosed in a message or enum.顶层不能有字段,所有内容都应包含在消息或枚举中。 So in your case you should have something like:所以在你的情况下你应该有这样的东西:

message Foo {
    repeated int32 items = 1;
}

message Bar {
    repeated Foo items = 1;
}

Then you will be able to set the items by doing the following:然后您将能够通过执行以下操作来设置项目:

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

where pb is the name for the import of the package you defined in your proto file.其中 pb 是您在 proto 文件中定义的 package 的导入名称。 Eg:例如:

proto:原型:

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

go: go:

import pb "example.com/m/proto"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM