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?
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.")
}
}
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
}