goprotocol-buffersprotobuf-netgrpc-goprotobuf-go

Protobuffers and Golang --- writing out marshal'ed structs and reading back in


Is there a generally accepted "correct" way for writing out and reading back in marshaled protocol buffer messages from a file?

I've been working on a smaller project that simulates a full network locally with gRPC and am trying to add writing to/ reading from files s.t. I can save state and start from there when its launched again. It seems I was naive in assuming these would remain on a single line:

Sees chain of length 3

from debugging messages I've written; but,

$ wc test.dat
   7    8 2483 test.dat

So, I suppose there are an extra 4 newline's... Is there a method of delimiting these that I can use? or do I need to come up with one on my own? I realize this is straightforward, but in my mind, I can only probabilistically guarantee that <<<<DELIMIT>>>> or whatever will never show up and put me back at square 1.


Solution

  • Use proto.Marshal/Unmarshal:

    That way you simulate (closest) to receiving the message while avoiding side effects from other Marshal methods.

    Alternative: Dump it as []byte and reread it.