> Can you just have a field of your Context type be a value of type
> context.Context?
>
> Ian
>
I managed to do this eventually. The confusion was that my library has
multiple types of contexts and they need to be stacked in any particular
order. So at first, the values from previous (parent) contexts did not seem
to transfer well to the child contexts. When looking at the context.Context
source code to learn how it does that, I was surprised to learn that it
propagates those cancellation signals down to its children manually, which
is yucky.
What I ended up doing is create another Context interface for my package
that has all the functions I need. Create a parent struct that implements
those functions with empty features. Then each feature context can overload
those functions as needed.
package yourpackage
import "context"
type Context interface{
context.Context
FeatureX() *X
FeatureY() *Y
FeatureZ() *Z
}
type BasicContext struct {
context.Context
}
func (ctx *BasicContext) FeatureX() *X {
return nil;
}
func WithBasicContext(parent context.Context) Context {
return &BasicContext{Context: parent}
}
type featureXContext struct {
Context
x *X
}
func (ctx *featureXContext) FeatureX() *X {
return ctx.x
}
func WithFeatureX(parent Context, x *X) Context {
return &featureXContext{Context: parent, x: x}
}
It seems to be functioning as expected, and compatible with the other
contexts like WithDeadline or WithTimeout.
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.