-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: Go 2: For Else branch #41348
Comments
Fwiw, such a construct is possible today without the bool or resorting to assembly: for _, v := range haystack {
if v == needle {
goto next
}
}
// Operation when the set lacks the item
next:
// either way, we continue here This sort of Pythonic for/else syntax would merely by sugar. |
Why not write a |
For language change proposals, please fill out the template at https://go.googlesource.com/proposal/+/refs/heads/master/go2-language-changes.md . When you are done, please reply to the issue with Thanks! |
Compare #24282 |
This comment has been minimized.
This comment has been minimized.
I often find I need the following if len(values) == 0 {
... // print some info
} else for _, v := range values {
...
} Surely, currently we can write it as if len(values) == 0 {
... // print some info
}
for _, v := range values {
...
} But I think the former is more logical (and could be a bit more efficient). If if-block can follow [edit]: a better example for the occasion: if vs := f(); len(vs) == 0 {
} else {
for _, v := range vs {
}
}
// vs.
if vs := f(); len(vs) == 0 {
} else for _, v := range vs {
} |
I'm personally just not a fan of |
Timed out in state WaitingForInfo. Closing. (I am just a bot, though. Please speak up if this is a mistake or you have the requested information.) |
For followers, the GOTO approach works:
https://play.golang.org/p/8ha8Spl4vBG
So there's no need for "isFound" variables nor the setting or checking of
them.
…On Sun, Oct 11, 2020 at 3:17 PM GopherBot ***@***.***> wrote:
Closed #41348 <#41348>.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#41348 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAOU4LTY3KHQJRTMAOMI5MTSKIAGBANCNFSM4RIBYJRA>
.
|
While that works in that specific example, the general case is a bit more complicated: https://play.golang.org/p/wMaykOGCfMC The code requires 2 gotos which cross over each other, which can quickly lead to spaghetti code if there is more code in each section other than a simple |
|
Looking back at this thread, I'm not sure if anyone's pointed out the precedent of text/template's |
Whenever a Golang program needs to determine a property of all of a set/list/map/chan, it requires an "if" branch operation on a "found" variable at the end of a loop. Ex:
With a For-Else construct, this could save a variable, a branch, 3 lines, and has good readability:
The assembly would be as simple as having break jump to a label below where the end of the For loop jumps to.
Go's overloaded For construct could allow this with Range and it can work with a condition. It has no interesting meaning for unconditional looping. This idea is borrowed from Python.
The text was updated successfully, but these errors were encountered: