I'm just getting started with CGo and I'm trying to send data to a C library that performs statistical computations on arrays of floats/doubles. What I'm trying to figure out right now is how to send an array of floats, or C.double's, to a CGo function that has a signature like this:
double pop_mean(int numPoints, double a)
I've figured out how to get in the C.int in there, but I'm having trouble figuring out how to send in an array of doubles.
I haven't yet seen any blog posts or SO Questions about this exact thing, so I thought I'd ask.
The following is my best effort so far.
// Get a basic function to work, while passing in an ARRAY arr := make(C.double, 0) arr = append(arr, C.double(10.0)) arr = append(arr, C.double(20.0)) arr = append(arr, C.double(30.0)) var fixedArray C.double = arr[:] // ptr := C.CBytes(arr) // defer C.free(unsafe.Pointer(ptr)) coolMean := C.pop_mean(3, &fixedArray) fmt.Println("pop_mean (10, 20, 30): ", coolMean)
And this is the error I'm getting:
./main.go:64:6: cannot use arr[:] (type _Ctype_double) as type _Ctype_double in assignment ./main.go:69:35: cannot use &fixedArray (type *_Ctype_double) as type *_Ctype_double in argument to _Cfunc_pop_mean
How should I be passing an array of C.double to the code?