-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: all: unify context
and return err
idioms for error handling
#69088
Comments
This feels like reinventing C's errno system. Conceptually, you can split up functions into:
I appreciate then that once context is involved, you always also need errors, but I don't think obscuring the return type helps. These are different kinds of functions, they should have different kinds of signatures. If I were making a new language, I would use goroutine safe dynamic package variables instead of context and result types instead of error return tuples, but it's probably too late to change Go in either of those ways at this point, and it would be more than you could bolt on for a new standard library package. |
I'm not clear on what the proposal is. You are adding a As you say, we could only make such a change in a context/v2 package. |
context package is already handling too many reponsibilites and it's polluting every function. this will make it impossible to write any function without the god-like context |
A major problem with go error handling is repeat code such as: if err!=nil{
return nil,err
} The proposal does not solve that. The API with and without a receive context has swollen the number of standard library apis, and if try func(...) (T,error) and func(context,...) (T) Two different forms, adding API is a lot of work, not worth the loss. |
Closing due to unanimous disapproval (see emoji votes) |
Proposal Details
Go is capable of achieving concise and elegant error handling today, without the need for any language changes.
The following code is the archetypal
CopyFile()
example, rewritten in terms of a unifiedcontext
andreturn err
pattern:Given that a v2 of the standard library is in the works, this is probably the best time to consider an API overhaul like this.
On the other hand,
func(...) (T,error)
may be too ingrained into Go culture at this point for a change like this.The text was updated successfully, but these errors were encountered: