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

Why is go fmt soft on newlines in between code? #43665

Closed
prasad83 opened this issue Jan 13, 2021 · 3 comments
Closed

Why is go fmt soft on newlines in between code? #43665

prasad83 opened this issue Jan 13, 2021 · 3 comments

Comments

@prasad83
Copy link

go fmt standardization of code is good in most cases but it goes soft on newline usage within code.

Why not have (one newline after statement and one newline before block) as a standard. This will avoid code uniformity.

Example:

var myVar string


myVar = "Here is next statement"

if myVar != "" {
    myVar = "Here in the if block"
}
println(myVar)

Would become

var myVar string
myVar = "Here is next statement"

if myVar != "" {
   myVar = "Here in the if block"
}

println(myVar)
@davecheney
Copy link
Contributor

IMO newlines are often used like paragraphs in a book, they separate distinct ideas. How would you suggest this fragment be formatted?

err := fn()
if err != nil { 
   // clean up
   return err
}

@lu4p
Copy link

lu4p commented Jan 13, 2021

I agree that go fmt should enforce some newlines, I'm not sure I agree with the rules you came up with, as this would mean that simple error checks have a new line before them.

  • newline after a block ({}\n) or multiple continious blocks (if {} else{}\n), this means a newline after every error check

  • the newline isn't added if the following line also closes a block

func main() {
    for {
    } // no newline here
}

There might be other cases where it makes sense to inject a newline.

I think this is highly opinonated and we should explore this with gofumpt first.

@mdlayher
Copy link
Member

Please see https://github.com/golang/go/wiki/Questions. Closing.

@golang golang locked and limited conversation to collaborators Jan 13, 2022
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

5 participants