Created
June 15, 2017 09:01
-
-
Save denisvmedia/4a18b60e58de42cff3e91ce9986cd325 to your computer and use it in GitHub Desktop.
Quit by closing channel
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 "sync" | |
import "fmt" | |
import "time" | |
func main() { | |
quit := make(chan interface{}, 10) | |
wg := sync.WaitGroup{} | |
wg.Add(1) | |
go func() { | |
for { | |
select { | |
case <-quit: | |
fmt.Println("exited") | |
wg.Done(); | |
return | |
default: | |
time.Sleep(time.Second / 2) | |
fmt.Println("waiting") | |
} | |
} | |
}() | |
time.Sleep(time.Second * 20) | |
close(quit) | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment