Help with Python bindings

blov · 2017-11-01 16:00:11 · 611 次点击    
这是一个分享于 2017-11-01 16:00:11 的资源,其中的信息可能已经有所发展或是发生改变。

Hey gophers,

I'm currently in the process of writing a genetic programming lib for machine learning in Go. I'm nearly done with coding bindings for Python but I just have one last problem.

The Python package is here. In the binding.py file I'm calling an exporting function called Predict located in main.go. The problem is that Predict returns a []float64 slice. This causes the following panic when the method is called from Python: panic: runtime error: cgo result has Go pointer.

My question is thus: is there any way to return Go slices when making a call from another language?

I'm sorry if my question is misplaced. I wasn't too sure where to ask (I'm not used to mail groups).

By the way I'm looking for contributors, there's a lot of work to be done and I'm getting promising results on real world datasets!


评论:

raff99:

There is a similar answer here: https://stackoverflow.com/questions/41492071/how-do-i-convert-a-go-array-of-strings-to-a-c-array-of-strings

It's for array of strings, but it's the same for array of floats. Your Go code needs to allocate (with C.malloc) the return array and copy the Go values into.

recurrency:

very cool! I have tried your gago package and really liked it, the api was very elegant.

don't have an answer to your question – but maybe try in #cgo or #data-science on the gophers slack?

Lemax0:

Thanks! Glad you like it.

I didn't think about Slack, good call.


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

611 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传