Skip to content

Instantly share code, notes, and snippets.

@lupguo
Created June 1, 2019 15:42
Show Gist options
  • Save lupguo/cf18ef8148b40e616d269ecaa72ecc3d to your computer and use it in GitHub Desktop.
Save lupguo/cf18ef8148b40e616d269ecaa72ecc3d to your computer and use it in GitHub Desktop.
ticker example in go
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