dsfhe34889789708
2018-06-11 12:17 阅读 47
已采纳

超时后如何取消fmt.Scanf?

I have a very simple command line utility and at the end of it I'm waiting for a user to hit the enter key to end the program:

fmt.Scanf("
") // wait for hitting the enter key to end the program

Now I want to change it and if the enter key hasn't been hit for some time, I want to cancel Scanf and do something else. Is it possible to cancel waiting for user's input?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    drug95107 drug95107 2018-06-11 13:04

    You can simply create a channel and launch a goroutine which does the fmt.Scanf(" ") and then writes something to the channel. Then select between that channel and time.After(3 * time.Second).

    Here's a clean solution:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        // Rest of the program...
    
        ch := make(chan int)
    
        go func() {
            fmt.Scanf("
    ")
            ch <- 1
        }()
    
        select {
        case <-ch:
            fmt.Println("Exiting.")
        case <-time.After(3 * time.Second):
            fmt.Println("Timed out, exiting.")
        }
    }
    
    点赞 评论 复制链接分享
  • dsf323233323332 dsf323233323332 2018-06-11 12:34

    You just need to put it in an infinate loop

    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
    }
    
    点赞 评论 复制链接分享

相关推荐