Quit Channel

Learn how to stop a goroutine.

Overview of the Quit channel

One goroutine can’t forcibly stop another. A gorou­tine can stop only by returning from its top-level function. We can’t kill a goroutine from outside. We can signal to a goroutine to stop using a channel. For example, suppose our goroutine looks like the following:

ch := make(chan string, 10)
go func(){
    for {
        ch <- do_stuff()
    }
}()

The channel will block once the buffer is full. But we didn’t kill the goroutine, stop it, or return from it. Once the buffer gets emptied, it will start working again.

Stop the goroutine

If we want to exit from the goroutine, we need to send an external interrupt signal to stop the goroutine.

Get hands-on with 1200+ tech skills courses.