Open FGasper opened 1 year ago
This case was considered before, but later we decided on the standard for using custom context: Instead of directly using this custom struct, we should pass it through context.Context and retrieve the custom structure using ctx.Value, or use both ctx and the custom struct as two parameters. Using the custom struct to pass context is not very intuitive in the code.
If I’m understanding you correctly, you’re saying that only the standard library should really implement the context.Context
interface. The standard library’s own documentation makes no such claim, so I’m a bit surprised at the notion.
For better or for worse, my own project has a custom implementation, and we can’t easily change that.
Would you accept a PR to add this flexibility, or do you prefer only to support the standard library’s Context?
I must have missed one scenario. 😭
func Do(ctx context.Context, args int) {
customCtx := ctx.Value(customKey{})
}
func Use() {
ctx := context.WithValue(context.Background(), customKey{}, new(customCtxType))
Do(ctx, 1)
}
var _ context.Context = new(customCtxType)
func Do(ctx context.Context, args int) {
customCtx := ctx.(*customCtxType)
}
func Use() {
customCtx := new(customCtxType)
Do(customCtx, 1)
}
var _ context.Context = new(customCtxType)
func Do(ctx context.Context, customCtx *customCtxType, args int) {
}
func Use() {
customCtx := new(customCtxType)
Do(customCtx, customCtx, 1)
}
The following fails when I plug it into contextcheck’s test suite: