Skip to content

Instantly share code, notes, and snippets.

@lupguo
Created June 1, 2019 15:42
Show Gist options
  • Select an option

  • Save lupguo/cf18ef8148b40e616d269ecaa72ecc3d to your computer and use it in GitHub Desktop.

Select an option

Save lupguo/cf18ef8148b40e616d269ecaa72ecc3d to your computer and use it in GitHub Desktop.

Revisions

  1. lupguo created this gist Jun 1, 2019.
    39 changes: 39 additions & 0 deletions ticker_example.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    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")
    }