Skip to content

Instantly share code, notes, and snippets.

@innermond
Created January 3, 2018 17:39
Show Gist options
  • Save innermond/eaa2cba4aecc6fb1a644b19c9dfab651 to your computer and use it in GitHub Desktop.
Save innermond/eaa2cba4aecc6fb1a644b19c9dfab651 to your computer and use it in GitHub Desktop.
Pulsing goroutine
import (
"bufio"
"fmt"
"io"
"os"
"time"
)
func main() {
quit := make(chan bool)
screen := bufio.NewWriter(os.Stdout)
go pulse(screen, quit)
time.Sleep(2 * time.Second)
quit <- true
}
func pulse(w io.Writer, q chan bool) {
var tk *time.Ticker
{
tk = time.NewTicker(2 * time.Second)
}
for {
select {
case <-tk.C:
fmt.Println("tick\n")
w.WriteString(fmt.Sprintf("%s\n", tk.C))
case <-q:
tk.Stop()
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment