简体   繁体   中英

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. 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 :

Go struct:

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

protobuf schema:

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 模式即可为您(以您最喜欢的语言)生成所有数据结构

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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