-
Notifications
You must be signed in to change notification settings - Fork 138
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
Ignore errors whose return value is named _ #130
Comments
I'm not sure this is viable. Some Go code ends up giving the error return value blank identifier name for completely unrelated reasons. E.g., imagine you start with: type Bar struct { ... }
func Foo() (Bar, error) {
...
} Then you need to add another return value of type func Foo() (Foo, string, error) {
...
} However, that reads poorly in terms of documentation, it's not clear what the meaning of the func Foo() (Foo, description string, error) {
...
} That doesn't compile because named and unnamed return values are mixed, so to avoid changing the rest of the code, you give blank identifier names to the other variables: func Foo() (_ Foo, description string, _ error) {
...
} There is existing Go code that uses blank identifier names in return variables. It's relatively rare, but it exists, including in the Go standard library. That's why I don't think it's viable to try to give it special meaning. People who wrote code like that in the past may not have expected it to mean something. |
There are two ways:
|
@shurcooL yes, the purpose is to set an enforceable convention for when you must return an error to satisfy a type signature, like with interfaces, but will never return an error because you do not need to. A comment is great for people, but unless that's in a standardized format it would be hard for tools to extract that information so you end up with white lists that exist outside the codebase. It would be nice if it could just be in the code. The meaning, or rather meaninglessness, of If someone writes code that uses |
I'm not in agreement with that. I think using |
That's awful |
Here's an example of it being used (in good taste, IMO) in the standard library: The That code certainly does return non-nil errors, despite having This is just an example, and the above is just my opinion. |
I'd send a patch to Go to fix that if it weren't such a hassle. |
If I were reading code out loud, I'd pronounce This is the only context where you can name something |
We can use
_ = f()
to explicitly ignore a returned error in code, but there is no means to specify in code thatf
never returns an error.However, this is legal:
Explicitly naming the error return to discard mirrors the convention of assigning to discard. As a bonus, godoc lists named returns so code following this pattern is easily recognizable from its signature. (It could of course be lying and actually return an error, but that seems like something to be detected by a different linter).
From discussion on golang/go#20803 related to #114
I'd be happy to make a PR, though looking at the code it's not immediately obvious where to integrate this.
The text was updated successfully, but these errors were encountered: