Created
April 8, 2019 14:44
-
-
Save gocs/675415a6ee6876bc71e287018195e570 to your computer and use it in GitHub Desktop.
goroutine that counts while sleeping
This file contains 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 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