I am using the GOB encoding for my project and i figured out (after a long fight) that empty strings are not encoded/decoded correctly. In my code i use a errormessage (string) to report any problems, this errormessage is most of the time empty. If i encode a empty string, it become nothing, and this gives me a problem with decoding. I don't want to alter the encoding/decoding because these parts are used the most. How can i tell Go how to encode/decode empty strings?
Example: Playground working code. Playground not working code.
The problem isn't the
encoding/gob
module, but instead the customMarshalBinary
/UnmarshalBinary
methods you've declared forMsg
, which can't correctly round trip an empty string. There are two ways you could go here:Get rid of the
MarshalBinary
/UnmarshalBinary
methods and rely on GOB's default encoding for structures. This change alone wont' be enough because the fields of the structure aren't exported. If you're happy to export the fields then this is the simplest option: https://play.golang.org/p/rwzxTtaIh2Use an encoding that can correctly round trip empty strings. One simple option would be to use GOB itself to encode the struct fields:
You can experiment with this example here: https://play.golang.org/p/oNXgt88FtK
The first option is obviously easier, but the second might be useful if your real example is a little more complex. Be careful with custom encoders though: GOB includes a few features that are intended to detect incompatibilities (e.g. if you add a field to a struct and try to decode old data), which are missing from this custom encoding.