Skip to content

Instantly share code, notes, and snippets.

@emmiep
Last active March 16, 2019 14:59
Show Gist options
  • Select an option

  • Save emmiep/662c64914ad3d2e1719e28aba417a53b to your computer and use it in GitHub Desktop.

Select an option

Save emmiep/662c64914ad3d2e1719e28aba417a53b to your computer and use it in GitHub Desktop.
Periodically do background task while waiting for result
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