Does Context.Done() unblock when context variable goes out of scope in golang?
go
Solution
No, it doesn't cancel automatically when the context leaves scope. Typically one calls `defer cancel()` (using the callback from `ctx.WithCancel()`) oneself to make sure that the context is cancelled.
https://blog.golang.org/context provides a good overview of how to use contexts correctly (including the `defer` pattern above). Also, the source code https://golang.org/src/context/context.go is quite readable and you can see there's no magic that would provide automatic cancellation.
Problem
Will context.Done() unblock when a context variable goes out of scope and cancel is not explicitly called? Let's say I have the following code: ``` func DoStuff() { ctx, _ := context.WithCancel(context.Background()) go DoWork(ctx) return } ``` Will ctx.Done() unblock in DoWork after the return in DoStuff()? I found this thread, https://groups.google.com/forum/#!topic/golang-nuts/BbvTlaQwhjw, where the person asking how to use Context.Done() claims that context.Done() will unblock when the context variable leaves scope but no one validated this, and I didn't see anything in the docs.