Skip to content

Instantly share code, notes, and snippets.

@gocs
Created April 8, 2019 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gocs/675415a6ee6876bc71e287018195e570 to your computer and use it in GitHub Desktop.
Save gocs/675415a6ee6876bc71e287018195e570 to your computer and use it in GitHub Desktop.
goroutine that counts while sleeping
package main
import (
"fmt"
"time"
)
func count(quit chan bool) {
count := 0
for {
select {
case <-quit:
fmt.Println("\n\t\ttotal:", count)
return
default:
}
count++
fmt.Print(".") // to print the `quit` reciever case
}
}
func main() {
quit := make(chan bool)
go count(quit)
time.Sleep(time.Second / 100)
quit <- true // forces `count` to do job
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment