如何在超时后取消fmt.Scanf?

时间:2018-06-11 12:17:59

标签: go

我有一个非常简单的命令行实用程序,最后我正在等待用户点击回车键结束程序:

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

现在我想更改它,如果输入键还没有被击中一段时间​​,我想取消Scanf并做其他事情。是否可以取消等待用户的输入?

2 个答案:

答案 0 :(得分:4)

您可以简单地创建一个频道并启动一个执行fmt.Scanf("\n")的goroutine,然后将某些内容写入频道。然后在该频道与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.")
    }
}

答案 1 :(得分:2)

你只需要把它放在一个无限循环中

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
}