4
我正在關注this教程,並介紹了將Go結構序列化/編組爲協議緩衝區的部分。我的結構有一張地圖,我找不到任何有關如何處理編組地圖的文檔。如何將Go地圖序列化爲protobuff
在我想要序列以下Fields map[string]string
:
轉到結構:
type Note struct {
ID NoteID
Fields map[string]string
}
protobuf的模式:
package internal;
message Note {
optional int64 ID = 1;
optional map<string, string> Fields = 2;
}
轉到名帥:
func MarshalNote(n *remember.Note) ([]byte, error) {
return proto.Marshal(&Note{
ID: proto.Int64(int64(n.ID))
Fields: proto.???
})
}
我不知道該怎麼做最後一行,我搜索的任何內容都是關於將字段映射到protobuf方案的討論,而不是將映射映射到protobuf方案。
雖然我傾向於同意Yandry在這裏,我也會讓原創爲我創建Go存根,而不是手動去做(這是一個重點它)我也有興趣做手動。在一天結束時,代碼必須在那裏,因爲automagic世代使用它,也許基於反思?我想這是星球大戰時間...使用源:) –
這裏有一個例子https://jacobmartins.com/2016/05/24/practical-golang-using-protobuffs/你可以檢查生成的代碼 –
對於地圖命名的子類的鍵和值類型的字符串,生成的標籤是這樣的:'protobuf:「bytes,1,rep,name = children」json:「children,omitempty」protobuf_key:「bytes,1,opt,name = key 「protobuf_val:」字節,2,opt,name = value「'。 –