Remix.run Logo
guappa a day ago

Yes it works, but you can't state the endianness and you have no control to decide if the compiler will decide to insert padding. It's undefined.

You HOPE it works.

mrbadguy 12 hours ago | parent [-]

I don’t know about the padding (certainly it never inserted any when I’ve used it) but you can definitely state the byte order upon reading or writing. That would definitely be an oversight. Take a look at the encoding/binary package:

https://pkg.go.dev/encoding/binary

guappa 10 hours ago | parent [-]

I want a struct, not to having to write the code manually to do a struct every single time.

I know you can do that in go but as I already said: "more error prone than C".

mrbadguy 9 hours ago | parent [-]

Can you please give me an example of what you don’t like? I’m not sure I understand the “write the code manually to do a struct” bit.

You have to define the struct for sure, but beyond that you just pass it to binary.Read and it comes back with the fields populated. I don’t see how you’d avoid defining the struct.

dfawcus 3 hours ago | parent [-]

I believe what he wants, is the usual C trick of defining a struct which represents the wire format (with all the usual caveats). Then cast a char pointer to be an instance of a pointer to that struct. Sort of like this:

    https://github.com/danos/vyatta-dataplane/blob/master/src/ecmp.c#L108-L116
It sort of works on x86 chips, but is not so effective on MIPS, PPC, etc where misaligned access are either unavailable, or slow, or even trap and are slower still.

Once one has to handle that sort of situation, and actually copy the data, the lack of language support for such type-punning becomes immaterial.