New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: context: add Context.IsCanceled method #39893
Comments
The proposed function is the same as if err := ctx.Err(); err != nil {
return err
} which seems preferable to me. |
As Ian pointed out, ctx.Err is exactly this function (returning an error instead of a boolean). |
While you could implement a func that is pure syntax sugar, I agree the justification seems weak: package context
func IsCanceled(ctx Context) bool { return ctx.Err() == Canceled } |
Based on the discussion above, this seems like a likely decline. |
There’s also the issue of a logical race, the context may be canceled after the call to IsCancelled but before the code responding to that condition has run. |
No change in consensus, so declined. |
Forgot to close in July. |
please add function like that to the context type:
func (c Context) IsCanceled() {
select {
case <- c.Done():
return true
default:
return false
}
}
or something like that to simplify context cancelation checking in channelless situations by something like that:
if ctx.IsCanceled() {
return ctx.Err()
}
The text was updated successfully, but these errors were encountered: