python部分如下:
```
def OnReceive(sockid, str_from_golang):
print str_from_golang
CCReceiveFUNC = CFUNCTYPE(None, c_int32, c_char_p)
gReceiveFunc = CCReceiveFUNC(OnReceive)
mydll = cdll.LoadLibrary
lib = mydll("./basego.so")
print gReceiveFunc
lib.setCallbackFunc(gReceiveFunc) 用go的setCallbackFunc()来设置python的函数,作为回调函数
go部分:
var gReceive func(int32,*C.char)
//export setCallbackFunc
func setCallbackFunc(pReceive func(int32,*C.char)){
gReceive = pReceive;
fmt.Printf("func addr:%x,%x,%x\n", &pReceive);
}
(gReceive)(1,C.CString(" receive some string, call python "))
运行到上面这句报错:unexpected fault address 0x0
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x7f90b0419acc]
```
golang做成一个so静态库,供python调用, 我想实现golang这边网络收到数据后回调python的函数,但是总是提示不成功,相同的在c++可以成功. 高手帮我瞧瞧.
dullblue · · 1200 次点击更多评论
````
没人知道吗?看来这里高手不多??
经过一天思考,现在已经解决了,供后面遇到这个问题的人少走弯路. 解决问题后整个人都轻松了,娃哈哈.
解决方案就是,用
/*
extern void func(a,b,funcpointer);
*/
import "C"
弄另外一个go文件来实现func函数,注意func()是c代码.必须分成连个go文件写,为什么?你遇到问题就知道了.
/*
void func(a,b,funcpointer)
{
funcpointer(a,b);
}
import "C"
然后在go中调用C.func(C.int(a),C.int(b),(*[0]byte)(unsafe.Pointer(funcpointer))
大功告成!!
```
#2