<p>Hi, <a href="/r/golang" rel="nofollow">r/golang</a>.I am reading an introductory golang book and I am stuck on a small detail.Basically the program is called 'dup' and it is supposed to look for adjacent duplicate lines from an input source(in this case os.Stdin).It is supposed to be inspired by the Unix <code>uniq</code> command.<a href="http://www.informit.com/articles/article.aspx?p=2453564&seqNum=3" rel="nofollow">This</a> is an exact replica of the code.I can get the program to compile with no problem but on running it, there is nothing.I can type text into the command line and hit enter etc but still nothing.I end up having to exit the program with no output.What am I missing?How am I supposed to run this program?Thanks!</p>
<hr/>**评论:**<br/><br/>sxan: <pre><p>There program reads from standard in, so you have to pipe a file to it, eg:</p>
<p>cat file.txt | ./dup</p>
<p>or</p>
<p>./dup < file.txt</p>
<p>Also, the program prints nothing if the input contains no duplicate lines.</p></pre>kamiligado: <pre><p>Thanks!</p></pre>mreithub: <pre><p>I assume you're talking about <code>dup1</code> here.
The program will read from <code>os.Stdin</code>until it reaches <code>EOF</code> (End of File), which for stdin only happens if you (as the other commenters suggest) pipe a file into the program, or if you hit Ctrl+D (at least on mac and linux, windows might be different)</p>
<p>Only after it finished reading the complete input file it'll print the statistics.</p></pre>kamiligado: <pre><p>Yup, thanks!Ctrl+D turns out to be the magic combination.I had tried everything else unsuccessfully.It is weird that the book has no mention of this at all.</p></pre>mhausenblas: <pre><p>The program reads from stdin, so you need to provide some input with duplicate lines, for example:</p>
<pre><code>$ cat test
something
else
duplicate
and
another
duplicate
$ cat test | go run main.go
2 duplicate
</code></pre></pre>kamiligado: <pre><p>Thanks!This works - I had no idea about the 'piping' at all and was running crazy :).</p></pre>vAltyR47: <pre><p>Yeah, I once filled out a bug report because gofmt (this was before the go tool) exhibited this behavior. It got closed as "working as intended" and I got a lecture in the Unix Philosophy from one of the Go devs.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传