-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: support "var identifier = _" to mute an identifier in certain scope. #17389
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
Comments
you can simply shadow your variables var n = 123
{
// use n
n := n // or var n = n
// now the real n can no longer be accessed
}
// here, n is still alive |
it is not totally the same |
How about const n = 1i // complex number or n_ := n
\\ no longer allowed to use/change n
if n != n_ {
panic("n was modified")
} I still don't get your use case. |
Maybe you want to solve it like this: |
Put this into any of your func use(...interface{}) {} Silent the unused variable(s) error anywhere by writing use(n, o, p) Bonus: The program does not build when not testing. |
a more detailed example:
It is like the "unset" in some languages. |
use unnamed vars var n int
_ = n var n int; _ = n |
I already got what you want to accomplish, but I don't get what it is good for. |
just let compiler report errors, just like the two error lines in my example. :) |
This is not my intention. |
@cznic, I don't get your code. |
It's also only for using unused vars, so not what you want. |
Sorry, previous discussions of overloading |
@bradfitz, I think this one is more reasonable. The underscore "_" is used in many cases now, but it is unnamed.
|
here is a useful case in reality:
|
You could do: db = nil. Or break the function up into smaller ones with more limited scopes. |
ye, there are some ways to avoid this problem, but the unset solution is cooler, ;) |
This is helpful to avoid using some identifiers which should be used in some scopes carelessly.
Example:
The text was updated successfully, but these errors were encountered: