Go: proposal: Go 2: extend "continue" to jump to start of function

Created on 6 Sep 2019  路  5Comments  路  Source: golang/go

continue statement begins the next iteration of the innermost "for" loop at its post statement. The "for" loop must be within the same function. If there is a label, it must be that of an enclosing "for" statement, and that is the one whose execution advances.

In addition to the above officially specified definition of continue keyword, I am proposing to extend its definition to also include the containing function body as the outer-most layer. That means:

  • "continue" goes to the start of the containing function if there is no enclosing for loop
  • "continue functionName" goes to the start of the containing function

I occasionally find myself using for {} or labels/gotos for an entire function, so I thought this would be a harmless and useful addition to Go. What do you think?

Note: When examining this, I realized we can create labels inside functions with the same name. So you can have a label main: inside main(). So I think it would be better to forbid such label names inside a function if this proposal is accepted.

Note 2: Since we have return, we don't need to change anything about "break".

FrozenDueToAge Go2 LanguageChange Proposal Proposal-FinalCommentPeriod

Most helpful comment

Within a loop, continue has a vaguely sensible meaning: continue with the next iteration of the loop. I can't see any way that continue implies that we should jump to start of the current function. A function is not a loop.

It also doesn't seem like something that comes up all that often. In https://blog.golang.org/go2-here-we-come Robert outlined three criteria for language changes. I'm not sure this meets criteria 1: address an important issue for many people.

All 5 comments

Within a loop, continue has a vaguely sensible meaning: continue with the next iteration of the loop. I can't see any way that continue implies that we should jump to start of the current function. A function is not a loop.

It also doesn't seem like something that comes up all that often. In https://blog.golang.org/go2-here-we-come Robert outlined three criteria for language changes. I'm not sure this meets criteria 1: address an important issue for many people.

As you noted, this can already be done using goto and a label. However, I would say that it's better in most cases to refactor the function and split the loop body into a separate function, that returns whether or not a continue is needed as a boolean.

"continue functionName" makes no sense. What you say is some kind of "repeat functionName" that makes no sense either. You can just run your function recursively or use labels.

This doesn't seem to have much, if any, support, so this looks like it'll be a likely decline.

Leaving open for four more weeks for final comments, though.

No final comments.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

go101 picture go101  路  3Comments

natefinch picture natefinch  路  3Comments

longzhizhi picture longzhizhi  路  3Comments

OneOfOne picture OneOfOne  路  3Comments

michaelsafyan picture michaelsafyan  路  3Comments