Go: Stop a goroutine

One goroutine can't forcibly stop another—a gorou­tine can stop only by returning from it's top level function.

To make a goroutine stoppable, let it listen to a stop signal on a channel. Here’s an example:

quit := make(chan struct{})
go func() {
	for {
		select {
		case <-quit:
			return
		default:
			// …
		}
	}
}()

// …
close(quit)

Sometimes it’s convenient to use a single channel for both data and signalling:

// Generator returns a channel that produces the numbers 1, 2, 3,…
// To stop the underlying goroutine, close the channel.
func Generator() chan int {
	ch := make(chan int)
	go func() {
		n := 1
		for {
			select {
			case ch <- n:
				n++
			case <-ch:
				return
			}
		}
	}()
	return ch
}

func main() {
	number := Generator()
	fmt.Println(<-number)
	fmt.Println(<-number)
	close(number)
	// …
}
1
2

Comments