英文:
How to represent golang "any" type in protobuf language
问题
我正在使用一个protobuf定义文件来创建golang类。
file.proto
message my_message {
<使用什么类型?> data = 1 [json_name = "data"]
}
generated.go
type MyMessage struct {
data any
}
我已经查看了Struct、Value和Any,但在生成的类中,它们都没有将类型标记为"any"。
我找到的最接近的是Value
类型,它可以容纳原始类型、结构体和列表。
英文:
am using a protobuf definition file to create golang classes
file.proto
message my_message {
<what type to use?> data = 1 [json_name = "data"]
}
generated.go
type MyMessage struct {
data any
}
I have checked out Struct , Value and Any, though none of them actually mark the type as “any” in generated classes.
The closes I get is Value
type which can accomodate primitives as well as structs and lists.
答案1
得分: 1
据我所知,Go代码生成器在生成字段时不会使用any
/interface{}
。在这里,你可能想要使用google.protobuf.Any
,但是生成的代码中的字段类型将是anypb.Any
。如果你确实需要使用any
类型,那么不幸的是你将不得不手动映射到另一个结构体。
英文:
As far as I'm aware the Go code generator doesn't have any circumstances where any
/interface{}
will be used for a generated field.
google.protobuf.Any
is likely what you want here, but the field in the generated code will of type anypb.Any
. If you absolutely require the any
type be used, you'll unfortunately have to manually map to another struct.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论