<p>So, </p>
<p>as you know (?), I decided to bring some golang into our 7-year-old rails project to break the ice and demonstrate the advantages of go and the easiest way to do this is to attach well-performing goroutines to ruby classes that really represent a bottleneck. In the future, we'll start to replace portions of our API with legit go. This is to say that I am aware that cgo is not go. :)</p>
<p>So my thinking is to create a word-parser "main" go file that imports two other files - just for organizing code - that will own the logic of parsing a word-cloud and a word-tree respectively. </p>
<p>Word-parser will provide ruby-attachable methods that delegate to the word-cloud and word-tree files. </p>
<p>My first attempt reminds me of ruby and I am pretty sure there ways to improve it. Things work until I try to concatenate strings in the deepest routines. I can guess the combination of pointers, go-strings and c-strings, but I would rather understand! Any suggestion? </p>
<p><a href="http://i.imgur.com/bXGD2VE.png">http://i.imgur.com/bXGD2VE.png</a></p>
<p>source <a href="https://github.com/etozzato/packaged">https://github.com/etozzato/packaged</a></p>
<p>EDIT: *the real-life code actually works. *</p>
<hr/>**评论:**<br/><br/>mc_hammerd: <pre><p>maybe an expert can answer you</p>
<p>i would suggest trying <code>"foo" + C.GoString(bar) + "baz"</code></p>
<p>from: <a href="https://utcc.utoronto.ca/%7Ecks/space/blog/programming/GoCGoStringFunctions" rel="nofollow">https://utcc.utoronto.ca/~cks/space/blog/programming/GoCGoStringFunctions</a></p></pre>mekdigital: <pre><p>that was easy! </p></pre>mekdigital: <pre><p>and this is the 'real life repo'</p>
<p><a href="https://github.com/etozzato/word-parser-go" rel="nofollow">https://github.com/etozzato/word-parser-go</a></p></pre>robvdl: <pre><p>I wonder if restructuring the code so that it doesn't need to re-run the regexp.MustCompile() statement each time in the matchSentences() function has any effect on performance, which is called in a loop from what I can see. It could also be that Go maintains a compiled regex cache in which case it might be an unnecessary optimisation, I don't know really.</p></pre>mekdigital: <pre><p>that is a very good observation. I can't even think that far :__)</p></pre>mortonpe: <pre><p>Hey Mek - I just sent you a pull request on the library: <a href="https://github.com/etozzato/packaged/pull/1" rel="nofollow">https://github.com/etozzato/packaged/pull/1</a>. </p>
<p>Ruby, C and GO have different representations of most data types. Your data flow is Ruby <-> C <-> Go. This means that your ruby library and your go library need to convert to and from the c data types in order to properly talk.</p>
<p>In this case, the ruby FFI gem converts ruby strings to C string before passing them to the shared object and converts the result to a ruby string before sending it back. (Magic) </p>
<p>On the GO side, your go wrapper function is expecting a go string when in fact you are sending it a C string. You need to accept and return *C.char and use C.CString and C.GoString to convert between go and c string.</p></pre>mekdigital: <pre><p>That is pretty awesome, I didn't think of "return ftm" :) </p>
<p>if you look at the 'real life example' I got pretty close to the C.CString/C.GoString handling! <a href="https://github.com/etozzato/word-parser-go" rel="nofollow">https://github.com/etozzato/word-parser-go</a></p>
<p>Thanks!</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传