英文:
Golang Protoc Message Variable Interface Type
问题
我的服务端点返回的响应如下:
type Response struct {
Value interface{}
ValueType string
}
我尝试编写一个 protoc 文件。在 Message 中,我应该如何替代 Value 的接口类型:
message ValueGetResponse {
string Value = 1;
string ValueType = 2;
}
英文:
My service endpoint has this reponse:
type Response struct {
Value interface{}
ValueType string
}
I try to write a protoc file. What should I write instead of Value has interface type in Message:
message ValueGetResponse {
string Value = 1;
string ValueType = 2;
}
答案1
得分: 3
你可以查看google.protobuf.Any
(https://developers.google.com/protocol-buffers/docs/proto3#any)
import "google/protobuf/any.proto";
message ValueGetResponse {
google.protobuf.Any Value = 1;
string ValueType = 2;
}
它不是interface{}
,但也允许更灵活的使用。
请注意你问题下面@blackgreen的评论。
英文:
You can have a look at google.protobuf.Any
(https://developers.google.com/protocol-buffers/docs/proto3#any)
import "google/protobuf/any.proto";
message ValueGetResponse {
google.protobuf.Any Value = 1;
string ValueType = 2;
}
It's not an interface{}
, but also allow to have more flexibility.
And pay attention to the comment from @blackgreen under your question.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论