How to serialize Go map to protobuff

I follow this tutorial and got to the serialization / marshaling part of Go structs in the protocol buffer. My structure has a map, and I cannot find documentation on how to handle map marshaling.

Next, I want to serialize the Fields map[string]string :

Go struct:

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

protobuf circuit:

 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โ€™m looking for to talk about matching a field with a proto-buf, and not about matching a map with a proto-buf.

+5
source share
1 answer

protobuf is a well-defined search format and one of the advantages of using it, it generates all data structures for you (in your favorite language) using the protobuf scheme, ei you do not need to do a manual marshal or brainless

+3
source

Source: https://habr.com/ru/post/1261475/


All Articles