I'm using golang to implement a task schedule and process system. I found that golang can't stop the goroutine externally, we must wait for the goroutine to end itself. I can stop a goroutine through a channel, however, the only time to check the value of the channel is when the select is called, and this must wait until the execution of the business function ends, which can take a long time.
for example: ticker := time.NewTicker(task.period) defer ticker.Stop() for { select { case <-task.channel: return case <-ticker.C: _ = task.task.Run(task.logger) } } The above code is executing in a goroutine, if I want to cancel this goroutine, I can send a signal to task.channel, but the signal only can be retrieved after the task.task.Run is finished, it may be a long time, such as 5 mins. Is there any good idea to cancel the goroutine like the above code as soon as possible? Thanks, Ethan -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/72dcd177-028e-43a3-aff9-6a6258e29bc4n%40googlegroups.com.