Help with Python bindings

blov · · 459 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey gophers,</p> <p>I&#39;m currently in the process of writing a <a href="https://github.com/MaxHalford/xgp">genetic programming lib for machine learning</a> in Go. I&#39;m nearly done with coding bindings for Python but I just have one last problem. </p> <p>The Python package is <a href="https://github.com/MaxHalford/xgp/tree/master/python-package">here</a>. In the <code>binding.py</code> file I&#39;m calling an exporting function called <code>Predict</code> located in <code>main.go</code>. The problem is that <code>Predict</code> returns a <code>[]float64</code> slice. This causes the following panic when the method is called from Python: <code>panic: runtime error: cgo result has Go pointer</code>.</p> <p>My question is thus: is there any way to return Go slices when making a call from another language?</p> <p>I&#39;m sorry if my question is misplaced. I wasn&#39;t too sure where to ask (I&#39;m not used to mail groups).</p> <p>By the way I&#39;m looking for contributors, there&#39;s a lot of work to be done and I&#39;m getting promising results on real world datasets!</p> <hr/>**评论:**<br/><br/>raff99: <pre><p>There is a similar answer here: <a href="https://stackoverflow.com/questions/41492071/how-do-i-convert-a-go-array-of-strings-to-a-c-array-of-strings" rel="nofollow">https://stackoverflow.com/questions/41492071/how-do-i-convert-a-go-array-of-strings-to-a-c-array-of-strings</a></p> <p>It&#39;s for array of strings, but it&#39;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.</p></pre>recurrency: <pre><p>very cool! I have tried your <code>gago</code> package and really liked it, the api was very elegant.</p> <p>don&#39;t have an answer to your question – but maybe try in #cgo or #data-science on the gophers slack?</p></pre>Lemax0: <pre><p>Thanks! Glad you like it.</p> <p>I didn&#39;t think about Slack, good call.</p></pre>

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

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