Often times I don't need all the elaborate machinery and verbosity of context.
For simple things like stopping a goroutine, I usually use a slight elaboration on closing a channel. This is a pattern I call "idempotent close". I've wrapped it up in a little library called "idem". Here is an illustration: a) the main state struct for the goroutine always has a member called Halt. import "github.com/glycerine/idem" type GoroutineState struct { Halt *idem.Halter } func NewGoroutineState() *GoroutineState { return &GoroutineState { Halt: idem.NewHalter(), } } b) The state machine for the goroutine is typically launched from a Start() function; it checks for shutdown with func (g *GoroutineState) Start() { go func() { for { select { case <- g.Halt.ReqStop.Chan: g.Halt.MarkDone() return case... // the rest of the state machine logic } } }() } c) Any number of clients can then safely shutdown the goroutine g: g.Halt.RequestStop() <- g.Halt.Done.Chan // optional, but use this if you need to know g is done -- 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. For more options, visit https://groups.google.com/d/optout.