Skip to content
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

context: document return values of Context.Deadline more precisely? #32650

Closed
acln0 opened this issue Jun 17, 2019 · 1 comment
Closed

context: document return values of Context.Deadline more precisely? #32650

acln0 opened this issue Jun 17, 2019 · 1 comment

Comments

@acln0
Copy link
Contributor

acln0 commented Jun 17, 2019

When threading a context.Context down to code which uses a net.Conn directly, I wanted to write the following code:

deadline, _ := ctx.Deadline()
if err := conn.SetDeadline(deadline); err != nil {
	// ...
}
// use conn

But because Context.Deadline does not document that deadline == time.Time{} when ok == false, I felt compelled to write this instead:

deadline, ok := ctx.Deadline()
if !ok {
	deadline = time.Time{}
}
if err := conn.SetDeadline(deadline); err != nil {
	// ...
}
// use conn

The current documentation says Deadline returns ok==false when no deadline is set, but does not mention anything about the value of deadline in that case.

Should it? Or is it self-evident on the basis that things which return (T, bool) generally return the zero value for T when the boolean is false?

I do realize that context.Context is an interface, but it would be nice to be able to rely on deadline == time.Time{} when the context has no associated deadline.

I propose changing the documentation to Deadline returns deadline == time.Time{} and ok == false when no deadline is set for clarification. If this is acceptable, I can send a CL.

@katiehockman
Copy link
Contributor

Deadline returns ok==false when no deadline is set should be enough. It wouldn't be expected that the value of deadline is anything meaningful or useful should ok be false, so that shouldn't require any additional documentation.

@golang golang locked and limited conversation to collaborators Jun 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants