简体   繁体   English

如何将 Go 地图序列化为 protobuff

[英]How to serialize a Go map into a protobuff

I'm following this tutorial and got to the part on serializing/marshaling Go structs into a protocol buffer.我正在关注教程,并开始将 Go 结构序列化/编组到协议缓冲区中。 My struct has a map and I can't find any documentation on how to handle marshaling a map.我的结构有一张地图,我找不到任何关于如何处理地图的文档。

In the following I want to serialize Fields map[string]string :在下面我想序列化Fields map[string]string

Go struct:去结构:

type Note struct {
    ID     NoteID
    Fields map[string]string
}

protobuf schema: protobuf 架构:

package internal;

message Note {
    optional int64 ID = 1;
    optional map<string, string> Fields = 2;
}

Go marshal:去元帅:

func MarshalNote(n *remember.Note) ([]byte, error) {
    return proto.Marshal(&Note{
        ID: proto.Int64(int64(n.ID))
        Fields: proto.???
    })
}

I have no idea what to do for the last line and anything I search for talks about mapping a field to a protobuf scheme, and not about mapping a map to a protobuf scheme.我不知道最后一行要做什么,我搜索的任何内容都是关于将字段映射到 protobuf 方案,而不是关于将地图映射到 protobuf 方案。

protobuf是一种定义明确的序列化格式,使用它的好处之一是只需使用 protobuf 模式即可为您(以您最喜欢的语言)生成所有数据结构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM