golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123k stars 17.54k forks source link

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

Closed prasad83 closed 3 years ago

prasad83 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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.

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 commented 3 years ago

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