C++ structure to golang

I send on TCP
struct Package{
__int32 Seq;
__int16 ResultCode;
__int32 AddCode;
char szText[64];
};

encoding/binary Read byts to golang struct,
but golang don’t have "char"
I mast use byte format in golang, but how I can convert ansi string to string golang?

Or if I will be use
struct Package{
__int32 Seq;
__int16 ResultCode;
__int32 AddCode;
wchar_t struct Package{
__int32 Seq;
__int16 ResultCode;
__int32 AddCode;
char szText[64];
};
szText wile be a rune[64]?

Since you are talking about sending over the network, I recommend defining the protocol in language agnostic terms, not basing it on the memory layout of C++. If you want to use a ready made solution, protocol buffers and a bunch of similar serializers are out there. Otherwise, just specify something like the message format in similar terms:

int32 Seq
int16 ResultCode
int32 AddCode
int32 TextLen
bytes Text  // TextLen number of bytes, no null termination

Make sure all integers are big endian. It’s then clear how the message is laid out on the wire and you can write the appropriate code on both sides.

2 Likes

Text in bytes in UTF-8?

The smallest unit which encodes an UTF-8 character is a byte. Therefore, yes, make TextLen the number of bytes it occupies. It will be so much easier to track the number of read bytes than the number of glyphs/runes, especially when you got invalid UTF-8 in it…

In general, a “byte” (or “octet” for the french among us) is the unit of measure in nearly every serialisation library I have tampered with. And that have been many across a broad collection of languages.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.