Skip to content
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

New semicolon rule breaks 'if then{} newline else{}' #416

Closed
gopherbot opened this issue Dec 12, 2009 · 5 comments
Closed

New semicolon rule breaks 'if then{} newline else{}' #416

gopherbot opened this issue Dec 12, 2009 · 5 comments

Comments

@gopherbot
Copy link

by ray@hollett.demon.co.uk:

I really like the new semicolon rules, but a standard layout pattern for
many people is to have 'else' on a new line.
As in:

if 1 == 2 { fmt.Println("line 1") }
else { fmt.Println("line 2") }

or:

if 1 == 2 {
    fmt.Println("line 1")
}
else {
    fmt.Println("line 2")
}

However both these cause a "syntax error near else". What now is required
is that the newline before the else is removed to give: } else {
This is what gofmt does, but I do not understand the reasons for it based
on the proposal given a few days ago for the new semicolon rules.
I notice that the Channels example in "Go For C++ Programers" is in the now
broken format:
if c.get { c.val = val; ch <- c }
else { val = c.val }

Personally I prefer a newline before else rather than "} else {", but I
prefer the new semicolon rules more.

What is your $GOOS?  $GOARCH?
linux (Ubuntu 9.10) 386 (8g)

Which revision are you using?  (hg identify)
e7f3b2a4d933 tip (I updated today)
@rsc
Copy link
Contributor

rsc commented Dec 12, 2009

Comment 1:

We're still in transition and much of the documentation
hasn't been updated yet.  (To avoid the inconsistencies
you can run "hg update release" to get back to the old state.)
We're aware of this specific problem, but it's not the
gofmt formatting anyway, and all the ways you might
imagine accommodating the alternate style complicate
things in bad ways.
You can convert your program using gofmt -w *.go.
> I do not understand the reasons for it based
> on the proposal given a few days ago for the new semicolon rules.
> I notice that the Channels example in "Go For C++ Programers" is in the now
> broken format:
> if c.get { c.val = val; ch <- c }
> else { val = c.val }
Allowing this would require accepting
   if c.get { c.val = val; ch <- c } ;
   else { val = c.val }
which would make the different pieces of
the if different statements.  That's pretty awkward.
An alternative would be to peek ahead in the
lexer and not insert ; before else, but that's
an extra rule that must be implemented by the
many tools and wouldn't have any benefit to
programs written in the standard formatting.
Complexity aside, there is a compelling benefit
to not looking ahead past a newline to decide
whether to insert a semicolon: it means that 
standard Go syntax can be used in newline-oriented
contexts like interactive environments without
any special cases.  (Compare this to Python's interpreter,
where an if statement must be followed by a 
blank line to get it to execute.)
For all these reasons, I think the most likely
outcome will be that the else behavior won't change.
If you'd like to discuss this more, the mailing list
is the right forum.

Owner changed to r...@golang.org.

Status changed to WontFix.

@peterGo
Copy link
Contributor

peterGo commented Dec 12, 2009

Comment 2:

This is likely to be another manifestation of the same problem.
Before gofmt, error message: 3: syntax error.
package main
func main()
{
    return
}
After gofmt, compiles.
package main
func main() { return }

@rsc
Copy link
Contributor

rsc commented Dec 12, 2009

Comment 3:

indeed.
gofmt to the rescue.

@rsc
Copy link
Contributor

rsc commented Dec 15, 2009

Comment 4:

Issue #430 has been merged into this issue.

@rsc
Copy link
Contributor

rsc commented Dec 15, 2009

Comment 5:

Issue #427 has been merged into this issue.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants