2016-10-26 34 views
4

我正在使用grpc golang在客戶端和服務器應用程序之間進行通信。 下面是protoc緩衝區的代碼。在gRPC protoc緩衝區中創建類型爲Map [string] interface {}的變量golang

syntax = "proto3"; 
package Trail; 

service TrailFunc { 
    rpc HelloWorld (Request) returns (Reply) {} 
} 

// The request message containing the user's name. 
message Request { 
    map<string,string> inputVar = 1; 
} 
// The response message containing the greetings 
message Reply { 
    string outputVar = 1; 
} 

我需要創建類型映射[字符串]界面{}消息數據結構,而不是圖[字符串]字符串內的一個字段inputVar。 我該如何實現它? 在此先感謝。

+0

副手,聽起來像「你不想要」。但我想'map '可能會工作,也許? – Vatine

回答

2

proto3具有類型Any

import "google/protobuf/any.proto"; 

message ErrorStatus { 
    string message = 1; 
    repeated google.protobuf.Any details = 2; 
} 

,但如果你看看它的實現,它僅僅是作爲

message Any { 
    string type_url = 1; 
    bytes value = 2; 
} 

你必須有可能使用反射和中間類型來定義自己這樣的消息。

example application

https://github.com/golang/protobuf/issues/60

0

雖然它變得有點冗長處理的「結構」式的協議緩衝區是可能接近golang的地圖[字符串]接口{}

但like interface {}將採用一些反射式開銷來確定實際存儲的類型是什麼。

例如見這裏評論:https://github.com/golang/protobuf/issues/370