There are some C objects like unions, structs that contain bitfields and structs whose alignment differs from Go's ABI, that cannot be accessed from Go. Some of these structures cannot be changed to be accessible from Go code as they are part of the API of an existing library.
To marshall such objects into Go structures we thus cannot really use Go code. Instead w have to write the marshalling code in C. This works fine but I have not found a feasible way to define C functions that operate on types defined in Go code. Right now I am defining the data types I am marshalling into on the C side and use these data types in my Go code.
This is really nasty if I want to expose the marshalled types as an API in my Go code, as I cannot expose a C type as a part of my package interface. My current approach involves remarshalling the already marshalled object into a type that is defined in Go code.
Is there a more elegant way to do what I want to do, i.e. marshalling C structs that cannot be accessed from Go code into data types defined in Go code?
As requested in the comment section, here is a collection of C objects that cannot be accessed from Go.
#include <complex.h>
#include <stdbool.h>
union foo {
int i;
float f;
};
struct bar {
bool x:1;
unsigned int y:3;
unsigned int z:4;
};
struct baz {
float f;
complex float c;
};
#pragma pack 1
struct quux {
char c;
short s;
int i;
};