A goroutine is a function which usually runs concurrently with other functions. If you want to stop goroutine, you pass a signal channel to the goroutine, that signal channel pushes a value into when you want the goroutine to stop.
The goroutine polls that channel regularly as soon as it detects a signal, it quits.
- Quit : = make (chan bool)
- go func ( ) {
- for {
- select {
- case <– quit:
- return
- default
- // do other stuff
- }
- }
- }()
- // Do stuff
- // Quit goroutine
- Quit <– true