I have a goroutine that calls a method, and passes returned value on a channel:
ch := make(chan int, 100)
go func(){
for {
ch <- do_stuff()
}
}()
How do I stop such a goroutine?
Typically, you pass the goroutine a (possibly separate) signal channel. That signal channel is used to push 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