1

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.

mmmeeeker
  • 138
  • 9

1 Answers1

0

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.

Norbert
  • 6,026
  • 3
  • 17
  • 40
  • Ah I should have been clearer/ posted code... that's what I've got, tried initially prepending some extra things for me to reconstruct the data structure wrappers with certainty and then cut down to just `proto.Marshal` and `proto.Unmarshal`; to be clear, my issue *seems* to be that the marshal'ed messages contain newline characters (not unrealistic... if I open in e.g. Vim even with a Nerd font, etc., still lots of question mark square char's/ rune's) – mmmeeeker Jul 24 '22 at 00:19
  • The marshalled data is []byte, so this might happen on byte alignment – Norbert Jul 24 '22 at 17:08