Golang Protoc 消息变量接口类型

huangapple go评论92阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2021年12月15日 20:26:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/70363671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定