我有一个非常简单的命令行实用程序,最后它在等待用户按下Enter键以结束程序:
fmt.Scanf("\n") // wait for hitting the enter key to end the program
Run Code Online (Sandbox Code Playgroud)
现在,我想更改它,如果一段时间未按回车键,我想取消Scanf并做其他事情。是否可以取消等待用户输入?
你只需要把它放在一个无限循环中
package main
import (
"fmt"
"time"
"context"
)
// 3 seconds for example
var deadline = time.Second * 3
func main() {
c := make(chan string, 1)
go scan(c)
ctx, _ := context.WithTimeout(context.Background(), deadline)
select {
case <-ctx.Done():
// didnt type for deadline seconds
case <-c:
// did it in time
}
}
func scan(in chan string) {
var input string
_, err := fmt.Scanln(&input)
if err != nil {
panic(err)
}
in <- input
}
Run Code Online (Sandbox Code Playgroud)
您只需创建一个通道并启动一个goroutine即可,fmt.Scanf("\n")然后将一些内容写入该通道。然后select在该频道和之间time.After(3 * time.Second)。
这是一个干净的解决方案:
package main
import (
"fmt"
"time"
)
func main() {
// Rest of the program...
ch := make(chan int)
go func() {
fmt.Scanf("\n")
ch <- 1
}()
select {
case <-ch:
fmt.Println("Exiting.")
case <-time.After(3 * time.Second):
fmt.Println("Timed out, exiting.")
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
464 次 |
| 最近记录: |