Created
January 3, 2018 17:39
-
-
Save innermond/eaa2cba4aecc6fb1a644b19c9dfab651 to your computer and use it in GitHub Desktop.
Pulsing goroutine
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
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