如何使用 gogo/protobuf 获取自定义 go 类型
How to get a custom go type using gogo/protobuf
我在 .proto 文件中的代码目前如下所示:
message Category {
int64 CategoryID = 1;
}
message Categories {
repeated Category cat = 1;
}
当我 运行 protoc --gogofaster_out=. *.proto
我得到的输出是:
type Category struct {
CategoryID int64
}
type Categories struct {
Cat []*Category
}
但我真正想要的是:
type Category struct {
CategoryID int64
}
type Categories []*Category
我的 .proto 文件中的代码需要是什么才能获得所需的输出?
Protobuf 基本上是一种序列化结构化数据的机制。这意味着在发送原型 'message' 之前,它必须被序列化。当你为不同的语言编译这个 proto 时,它会生成适当的 类(对于 c++/Java),Golang 的结构。在您的情况下, "type Categories []*Category" 不是一条消息,而是一个无法序列化的独立实体。 (我可以在这里纠正)。请参阅 Protobuf 语言指南 https://developers.google.com/protocol-buffers/docs/proto3
如果这里的意图是有一个Category类型的数组并序列化,建议封装在一个message中。
我在 .proto 文件中的代码目前如下所示:
message Category {
int64 CategoryID = 1;
}
message Categories {
repeated Category cat = 1;
}
当我 运行 protoc --gogofaster_out=. *.proto
我得到的输出是:
type Category struct {
CategoryID int64
}
type Categories struct {
Cat []*Category
}
但我真正想要的是:
type Category struct {
CategoryID int64
}
type Categories []*Category
我的 .proto 文件中的代码需要是什么才能获得所需的输出?
Protobuf 基本上是一种序列化结构化数据的机制。这意味着在发送原型 'message' 之前,它必须被序列化。当你为不同的语言编译这个 proto 时,它会生成适当的 类(对于 c++/Java),Golang 的结构。在您的情况下, "type Categories []*Category" 不是一条消息,而是一个无法序列化的独立实体。 (我可以在这里纠正)。请参阅 Protobuf 语言指南 https://developers.google.com/protocol-buffers/docs/proto3
如果这里的意图是有一个Category类型的数组并序列化,建议封装在一个message中。