Suppose I have these functions:
func A(ctx context.Context) {
// A takes some time to process
}
func B(ctx context.Context) {
// B takes some time to process
}
func C(ctx context.Context) {
// C takes some time to process
}
Note: Every function call takes some time to process.
Note: B should be called inside function A & C should be called inside function B.
Just need an example for this specific situation.
You just pass the same context as the argument, but whenever you wait for results from channelemote server\anything you use select statement with waiting for the <- ctx.Done(). Whenever the context would be canceled (by cancel function or due to the timeout), the internal chan would be closed and the receive operation always unblocks on the closed channel.
The general post about cancelation (without context, but mentally replace doneCh with ctx.Done()) https://blog.golang.org/pipelines
Talk about cancelation (the context example from page 15) https://talks.golang.org/2014/gotham-context.slide#15
Couple of examples https://www.sohamkamani.com/blog/golang/2018-06-17-golang-using-context-cancellation/