I'm new to Go and I wish to translate some C/C++ code I have to Go but I haven't been able to. The problem resides in 2 places: how can I initialize a struct I've defined and how to do a "memcopy"
The code I'm talking about is this: http://play.golang.org/p/e8N255qEAk line 69 and 74.
The C/C++ code I wanted to "translate" to Go is this:
typedef char xchr;
typedef int xint;
typedef double xdob;
typedef float xflt;
typedef struct
{
xint p;
xdob lat_lon_ele[3];
xflt psi_the_phi[3];
xflt gear_flap_vect[3];
}VEH1;
avio.p = 0;
avio.lat_lon_ele[0] = 47.460058;
avio.lat_lon_ele[1] = -122.32104;
avio.lat_lon_ele[2] = 8000.000;
avio.psi_the_phi[0] = 110.5;
avio.psi_the_phi[1] = 0.0;
avio.psi_the_phi[2] = 0.0;
avio.gear_flap_vect[0] = 1.0;
avio.gear_flap_vect[1] = 0.0;
avio.gear_flap_vect[2] = 0.0;
VEH1* av = &avio;
xchr data_send[6];
data_send[0]='V';
data_send[1]='E';
data_send[2]='H';
data_send[3]='1';
data_send[4]=0;
memcpy(&data_send[5],av,sizeof(VEH1)); // load in the data
The Go code looks like this:
type xchr int8
type xint int
type xdob float64
type xflt float32
type VEH1 struct {
p xint
lat_lon_ele [3]xdob
psi_the_phi [3]xflt
gear_flap_vect [3]xflt
}
type VEHA struct {
num_p xint
lat_lon_ele [10][3]xdob
psi_the_phi [10][3]xflt
gear_flap_vect [10][3]xflt
lat_view, lon_view, ele_view xdob
psi_view, the_view, phi_view xflt
}
var avio VEH1
avio = &VEH1{0, {47.460058, -122.32104, 8000.000}, {110.5, 0.0, 0.0}, {1.0, 0.0, 0.0}}
data_send := [6]xchr{'V', 'E', 'H', '1', 0, 0}
copy(data_send[5:5], avio);
Thank you!
So basically given these basic types:
You want to copy the bytes (memory representation) of a value of the following
struct
type:Note that the length of
int
in Go is platform dependent, it may be 32 bit or 64 bit depending on the target architecture you compile to. That would lead to platform dependent behavior, so let's fix it toint32
for now:This is how the byte sizes of the above
struct
is calculated. If you needint64
, just change it and add 4 additional bytes in size calculation.Next you want the result in an array whose element types are
xchr
. You need a big enough array, which is the "prefix" being"VEH1"
followed by the data of the abovestruct
. So it must have a size of4+sizeof(VEH1)
which is56
.In Go to copy the bytes of one type to the memory space of another type, you can do it the
unsafe
package and its generalPointer
type. Any pointer can be cast tounsafe.Pointer
andunsafe.Pointer
can be cast to any pointer type, so this is your "gateway" between different pointer types.You can take the address of a
struct
value ofVHE1
, convert it toPointer
, and convert the resultPointer
to a pointer to the target data type. Dereference the pointer and now you already have the value of the other type.The builtin
copy()
can only be used with slices, so first you need to slice your data arrays to be able to pass them tocopy()
.You can do it like this:
Output, wrapped to fit in screen:
Try the working demo on the Go Playground.
Having the result as
[]byte
If you want
[]byte
result (e.g. you want to write the result to anio.Writer
), then use[56]byte
type fordata_send
and of course castpavio
to*[52]byte
: