<p><a href="https://github.com/natefinch/pcgrep">https://github.com/natefinch/pcgrep</a></p>
<p>I wrote this in a half hour or so, in response to a friend saying he'd spent "several hours" writing something similar in python. Then someone told him about GNU parallel, and he figured he'd just do it in bash. But I thought it was an interesting problem, so I coded up a little solution. Far from complete, but it's functional and pretty quick (both to write and to run).</p>
<p>It's currently parallel per-file... I added code to run the regex matching in goroutines, which made it faster to search a single file, but the overhead of copying the line data to the goroutines actually slowed it down for num_files >= num_cores... possibly one could add more sophisticated logic to choose how parallel to go based on the number of files, but I was about done with spending time on it, since it wasn't really filling a need I had.</p>
<p>Figured I'd just post this here in case anyone is interested.... feel free to suggest optimizations, I didn't really spend much time optimizing other than trying out the aforementioned matching in goroutines.</p>
<hr/>**评论:**<br/><br/>natefinch: <pre><p>Oh, and a key thing I learned: when you call bufio.Scanner.Bytes(), you're given a slice of bytes that does no allocation, and if you try to use that slice asynchronously, it will get overwritten the next time you call Bytes(). Took me a while to figure out why I was getting odd output. Always read the docs, folks!</p></pre>volker48: <pre><p>That has bitten me as well.</p></pre>barsonme: <pre><p>Same.</p>
<p>I was building a trie and decided to use the Bytes method instead of Text and couldn't figure out why nothing was inside my trie.</p>
<p>RTFM is a good idea
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传