I have a go function that takes ...interface{} arguments and I need to call
a C function with pointers to (the value of) each of the arguments.
Is the following code safe (taking the address of the type switched value)?
Is there an better way of doing it?
(I am not concerned about what the C function does with the arguments, just
the invocation from go).
package main
// void f(void **args) { }
import "C"
import "unsafe"
func callf(args ...interface{}) {
params := make([]unsafe.Pointer, len(args))
for i := range args {
switch v := args[i].(type) {
case int32:
params[i] = unsafe.Pointer(&v)
case float32:
params[i] = unsafe.Pointer(&v)
case float64:
params[i] = unsafe.Pointer(&v)
default:
panic("invalid argument")
}
}
C.f(¶ms[0])
}
func main() {
callf(int32(1), float32(2.0), 3.0)
}
--