Last active
March 16, 2019 14:59
-
-
Save emmiep/662c64914ad3d2e1719e28aba417a53b to your computer and use it in GitHub Desktop.
Periodically do background task while waiting for result
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 ( | |
| "fmt" | |
| "time" | |
| ) | |
| func main() { | |
| c := make(chan string) | |
| t := time.NewTicker(200 * time.Millisecond) | |
| go func() { | |
| time.Sleep(1 * time.Second) | |
| c <- "Done!" | |
| }() | |
| for c != nil { | |
| select { | |
| case s := <-c: | |
| fmt.Printf("\n%s\n", s) | |
| c = nil | |
| t.Stop() | |
| case <-t.C: | |
| fmt.Print(".") | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment