Read user input until he press ctrl+c?

agolangf · · 430 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey, I am a beginner and I just learned how to read user input with scan and its variable passed by reference.</p> <p>for example, var s1, s2, s3 string n, _ := fmt.Scan(&amp;s1, &amp;s2, &amp;s3) fmt.Println(s1, s2, s3)</p> <p>However, how would you get user input when you don&#39;t know when he stops typing? I am trying to guide a user that he can stop by pressing ctrl+c, otherwise he could input as much as he want. </p> <p>Then, i came up with making use of infinite for loop and pass a single variable by reference to fmt.Scan. I do not know how to detect if he press ctrl+c. What kind of functions available for this in Go?</p> <hr/>**评论:**<br/><br/>echophant: <pre><p>Working example:</p> <pre><code> package main import ( &#34;fmt&#34; &#34;os&#34; &#34;os/signal&#34; &#34;syscall&#34; ) func main() { // Go signal notification works by sending `os.Signal` // values on a channel. We&#39;ll create a channel to // receive these notifications (we&#39;ll also make one to // notify us when the program can exit). sigs := make(chan os.Signal, 1) // `signal.Notify` registers the given channel to // receive notifications of the specified signals. signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) msg := make(chan string, 1) go func() { // Receive input in a loop for { var s string fmt.Scan(&amp;s) // Send what we read over the channel msg &lt;- s } }() loop: for { select { case &lt;-sigs: fmt.Println(&#34;Got shutdown, exiting&#34;) // Break out of the outer for statement and end the program break loop case s := &lt;-msg: fmt.Println(&#34;Echoing: &#34;, s) } } } </code></pre> <p>The signal code is taken from <a href="https://gobyexample.com/signals" rel="nofollow">Go by Example</a>. The key here is the select statement, which will wait until we receive input from either the user or the exit signal.</p></pre>tmornini: <pre><p>ctrl+c should NOT be used to end input, it should end the program.</p> <p>ctrl+d is EOF and pretty much universally signals end-of-input.</p></pre>JuggleTux: <pre><p>also EOF makes it possible to pipe input to the program</p></pre>FUZxxl: <pre><p>Traditionally, you would end user input by having the user type a dot on a line on its own. That&#39;s how <code>ed</code>, <code>sendmail</code> and various other tools work. Another thing you could do is letting the user end input by typing Ctrl+D (or Ctrl+Z on Windows). This causes the terminal driver to signalize that input has ended. Your program notices by receiving an <code>io.EOF</code> error. Reacting to Ctrl+C is a bit more complicated. There are two ways:</p> <ul> <li>Catch SIGINT by registering a signal handler using <a href="https://golang.org/pkg/os/signal/" rel="nofollow"><code>os.signal</code></a> and somehow notify the reading Go routine that input ended (e.g. using a channel)</li> <li>Reconfigure the terminal to accept Ctrl+C as the EOF marker for the duration of input using the <a href="http://golang.org/pkg/syscall/" rel="nofollow"><code>syscall</code></a> package and a lot of manual fiddling. Probably not worth the effort.</li> </ul></pre>

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

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