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: spec: add init statement to switch cases #19774
Comments
I think a simple statements inside "if" is a bad idea. It too complex to understand that code.I never use that language feature. |
Is quite popular. |
It Is quite popular indeed. I prefer: err := fn()
if err != nil {
// handle error
} |
I'd like to add: case already supports simple statements under select: select {
case <-ch1:
// ...
case x := <-ch2
// ...use x
...
} which is widely used in go stdlib itself:
For me this is simply language consistency fix, not go2 matter... |
Send statement is the only simple statement a CommCase is specified to accept. |
case x := ...; x < 0: doSomething()
This makes the switch statement harder to understand when switching on a value. Consider
While we can define exactly what this means, it is just confusing. The advantage of being able to write the statement does not seem worth the complexity cost. |
Hello up there. Effective Go says:
for
if
statement the specification allows to have simple statements inside if for declaring/initializing variables, e.g. this way:and it works for if-else-if-else too.
However given a long if-else-if-else construct, in presence of simple statements inside if, it is not possible to rewrite it as switch-case-case because specification does not allow simple statements inside case - only inside switch.
For example I wanted to write something like this:
and could not. It is of course possible to use if-else-if-else chain, but the switch form looks more reasonable, and once again, Effective Go says it is idiomatic to write such chains as switch.
So I propose to extend language specification to allow simple statements inside
case
.Thanks beforehand,
Kirill
The text was updated successfully, but these errors were encountered: