Skip to content

proposal: Go 2: Do-While Extension to For-Loops #55868

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

Closed
assyrianic opened this issue Sep 26, 2022 · 1 comment
Closed

proposal: Go 2: Do-While Extension to For-Loops #55868

assyrianic opened this issue Sep 26, 2022 · 1 comment
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Milestone

Comments

@assyrianic
Copy link

assyrianic commented Sep 26, 2022

Author background

  • Would you consider yourself a novice, intermediate, or experienced Go programmer? --Intermediate.
  • What other languages do you have experience with? --C, C++, and Python.

Related proposals

  • Has this idea, or one like it, been proposed before? --Not that I've seen.
  • Does this affect error handling? --Possibly but shouldn't have too much of an effect.
  • Is this about generics? --No.

Proposal

  • What is the proposed change? --Extending the for-loop in Go to also be able to function as a do-while loop.
// traditional for-loop.
for i := 0; i < n; i++ {
    
}

// do-while-style for-loop.
for {
    
} if i < n /// <-- newline/semicolon inserted to end it.
  • Who does this proposal help, and why? --Provide a simpler mechanism for writing a do-while loop.
  • Please describe as precisely as possible the change to the language. --This would change the syntax grammar for the for-statement construct.
  • What would change in the language spec? --The ForStmt syntax.
  • Please also describe the change informally, as in a class teaching Go.

At the end of a for-loop statement, an optional if and a conditional expression following the if would allow the for-loop to act similar to a do-while loop. The conditional would work as an "after-check" when the code block of the for-loop finished an iteration.

  • Is this change backward compatible? --Yes.
  • Orthogonality: how does this change interact or overlap with existing features? --It does not.
  • Is the goal of this change a performance improvement? --No.

Costs

  • Would this change make Go easier or harder to learn, and why? --Debatebly Easier as the Go For-loop would be the all encompassing loop.
  • What is the cost of this proposal? (Every language change has a cost). --Needing to work on any parsing such as the go/ast library.
  • How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected? --Any tool that has to analyze/manipulate the source code.
  • What is the compile time cost? --Insignificant.
  • What is the run time cost? --None.
@gopherbot gopherbot added this to the Proposal milestone Sep 26, 2022
@ianlancetaylor ianlancetaylor added LanguageChange Suggested changes to the Go language v2 An incompatible library change labels Sep 26, 2022
@rittneje
Copy link
Contributor

rittneje commented Sep 26, 2022

You can already essentially do this today:

for {
    // ...
    if i >= n {
        break
    }
}

@golang golang locked and limited conversation to collaborators Sep 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

4 participants