在Go语言终端应用程序中以编程方式结束输入。

huangapple go评论116阅读模式
英文:

end input programmatically in a golang terminal application

问题

我正在尝试在3秒内以编程方式结束终端输入并输出结果。

我的代码如下:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "time"
  7. )
  8. var (
  9. result string
  10. err error
  11. )
  12. func main() {
  13. fmt.Println("请输入一些内容,你有3000毫秒的时间")
  14. go func() {
  15. time.Sleep(time.Millisecond * 3000)
  16. fmt.Println("到了结束输入的时间,读取你已经输入的内容")
  17. fmt.Println("result")
  18. fmt.Println(result)
  19. }()
  20. in := bufio.NewReader(os.Stdin)
  21. result, err = in.ReadString('\n')
  22. if err != nil {
  23. fmt.Println(err)
  24. }
  25. }

输出结果:

  1. 请输入一些内容,你有3000毫秒的时间
  2. hello 到了结束输入的时间,读取你已经输入的内容
  3. result

我只打印了hello,然后经过了3秒,程序应该结束输入并读取我的hello并输出:

  1. result
  2. hello

但是我不知道如何实现这一点。是否可能在没有用户意图的情况下结束终端输入并读取输入的值?

英文:

I am trying to end terminal input programmatically in 3 seconds and output the result.

My code is the following:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "time"
  7. )
  8. var (
  9. result string
  10. err error
  11. )
  12. func main() {
  13. fmt.Println("Please input something, you have 3000 milliseconds")
  14. go func() {
  15. time.Sleep(time.Millisecond * 3000)
  16. fmt.Println("It's time to break input and read what you have already typed")
  17. fmt.Println("result")
  18. fmt.Println(result)
  19. }()
  20. in := bufio.NewReader(os.Stdin)
  21. result, err = in.ReadString('\n')
  22. if err != nil {
  23. fmt.Println(err)
  24. }
  25. }

The output:

  1. Please input something, you have 3000 milliseconds
  2. hello It's time to break input and read what you have already typed
  3. result

I just printed hello and 3 seconds passed and the program should end the input and read my hello and give output:

  1. result
  2. hello

But I don't know how to provide this. Is it possible to end terminal input without user's intention and read the inputted value?

答案1

得分: 2

你无法直接对标准输入流(stdin)设置超时,所以你需要在接收读取协程结果的过程中创建一个超时机制:

  1. func getInput(input chan string) {
  2. in := bufio.NewReader(os.Stdin)
  3. result, err := in.ReadString('\n')
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. input <- result
  8. }
  9. func main() {
  10. input := make(chan string, 1)
  11. go getInput(input)
  12. select {
  13. case i := <-input:
  14. fmt.Println(i)
  15. case <-time.After(3000 * time.Millisecond):
  16. fmt.Println("超时")
  17. }
  18. }

这段代码会创建一个名为input的字符串通道,并在一个单独的协程中调用getInput函数来读取标准输入。然后,通过select语句来等待输入结果或超时信号。如果在3秒内成功接收到输入结果,就会打印出来;否则,会打印"超时"。

英文:

You can't timeout the read on stdin directly, so you need to create a timeout around receiving the result from the reading goroutine:

  1. func getInput(input chan string) {
  2. in := bufio.NewReader(os.Stdin)
  3. result, err := in.ReadString(&#39;\n&#39;)
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. input &lt;- result
  8. }
  9. func main() {
  10. input := make(chan string, 1)
  11. go getInput(input)
  12. select {
  13. case i := &lt;-input:
  14. fmt.Println(i)
  15. case &lt;-time.After(3000 * time.Millisecond):
  16. fmt.Println(&quot;timed out&quot;)
  17. }
  18. }

huangapple
  • 本文由 发表于 2015年6月23日 23:02:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/31006442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定