Created
June 1, 2019 15:42
-
-
Save lupguo/cf18ef8148b40e616d269ecaa72ecc3d to your computer and use it in GitHub Desktop.
ticker example in go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bufio" | |
| "fmt" | |
| "os" | |
| "time" | |
| ) | |
| func main() { | |
| tick := time.NewTicker(1 * time.Second) | |
| interrupt := make(chan string) | |
| // input anything to interrupt ticker | |
| go func() { | |
| input := bufio.NewScanner(os.Stdin) | |
| for input.Scan() { | |
| tick.Stop() | |
| interrupt <- "over" | |
| } | |
| }() | |
| count := 5 | |
| printPoint: | |
| for { | |
| select { | |
| case t := <-tick.C: | |
| fmt.Println(t, count) | |
| if count--; count == 0 { | |
| break printPoint | |
| } | |
| case <-interrupt: | |
| break printPoint | |
| } | |
| } | |
| fmt.Println("over") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment