问题描述
- 这个go语言封装c函数,使用cgo,参数怎么传?
-
需要用go语言把下列的c函数封装一下,c的函数如下:
int raw_to_span(OPResult result, int *num, int **time, short **status, double **value);下面是我的写法,不正确,编译不通过:
func RawToSpan(result uintptr) (num int, time []int, status []int16, value []float64, ret int) {
var t = make([]C.int, num)
var s = make[]C.short, num)
var v = make([]C.double, num)
var n C.int
ret = int(C.raw_to_span(C.OPResult(result), &n, &t, &s, &v))
k := C.op2_num_rows(C.OPResult(result))
num = int(n)
for j := 0; j < int(k); j++ {
time = append(time, int(t[j]))
status = append(status, int16(s[j]))
value = append(value, float64(v[j]))
}
return
}
编译结果:
.api.go:533: cannot use &t (type *[]C.int) as type **C.int in argument to _Cfunc_op2_raw_to_span
.api.go:533: cannot use &s (type *[]C.short) as type **C.short in argument to _Cfunc_op2_raw_to_span
.api.go:533: cannot use &v (type *[]C.double) as type **C.double in argument to _Cfunc_op2_raw_to_span我也尝试了将t,s,v声明为:var t *C.int 这样写的确是在调用c的时候可以传进去了,但是返回值的时候怎么办?
在线等解答,谢谢
更多 0
解决方案
需要用go语言把下列的c函数封装一下,c的函数如下:
int raw_to_span(OPResult result, int *num, int **time, short **status, double **value);
下面是我的写法,不正确,编译不通过:
func RawToSpan(result uintptr) (num int, time []int, status []int16, value []float64, ret int) {
var t = make([]C.int, num)
var s = make[]C.short, num)
var v = make([]C.double, num)
var n C.int
ret = int(C.raw_to_span(C.OPResult(result), &n, &t, &s, &v))
k := C.op2_num_rows(C.OPResult(result))
num = int(n)
for j := 0; j < int(k); j++ {
time = append(time, int(t[j]))
status = append(status, int16(s[j]))
value = append(value, float64(v[j]))
}
return
}
编译结果:
.api.go:533: cannot use &t (type *[]C.int) as type **C.int in argument to _Cfunc_op2_raw_to_span
.api.go:533: cannot use &s (type *[]C.short) as type **C.short in argument to _Cfunc_op2_raw_to_span
.api.go:533: cannot use &v (type *[]C.double) as type **C.double in argument to _Cfunc_op2_raw_to_span
我也尝试了将t,s,v声明为:var t *C.int 这样写的确是在调用c的时候可以传进去了,但是返回值的时候怎么办?
在线等解答,谢谢