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

cmd/gofmt: make gofmt always insert a blank line between functions #22337

Closed
qinhanlei opened this issue Oct 19, 2017 · 4 comments
Closed

cmd/gofmt: make gofmt always insert a blank line between functions #22337

qinhanlei opened this issue Oct 19, 2017 · 4 comments
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.

Comments

@qinhanlei
Copy link

What version of Go are you using (go version)?

go version go1.8.3 darwin/amd64

What did you do?

I did gofmt on golang code as below.

package main
import "fmt"
func foo() {

	fmt.Println("this is foo!")
	// ...
	// may lots codes here
	// ...

}
func bar() {

	fmt.Println("this is bar!")
	// ...
	// may lots codes here
	// ...

}
func main() {

	fmt.Println("hello, world")
	foo()

	bar()

}

What did you expect to see?

package main

import "fmt"

func foo() {
	fmt.Println("this is foo!")
	// ...
	// may lots codes here
	// ...
}

func bar() {
	fmt.Println("this is bar!")
	// ...
	// may lots codes here
	// ...
}

func main() {
	fmt.Println("hello, world")
	foo()

	bar()
}

What did you see instead?

package main

import "fmt"

func foo() {

	fmt.Println("this is foo!")
	// ...
	// may lots codes here
	// ...

}
func bar() {

	fmt.Println("this is bar!")
	// ...
	// may lots codes here
	// ...

}
func main() {

	fmt.Println("hello, world")
	foo()

	bar()

}
@ALTree ALTree changed the title cmd/gofmt: May gofmt insert blank line between two functions is better ? cmd/gofmt: make gofmt always insert a blank line between functions Oct 19, 2017
@ALTree ALTree added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Oct 19, 2017
@ALTree
Copy link
Member

ALTree commented Oct 19, 2017

cc @griesemer

@mvdan
Copy link
Member

mvdan commented Oct 19, 2017

I'm not sure on the "always" part. For example, this sort of thing is useful:

func (n *Node) Noescape() bool { return n.flags&nodeNoescape != 0 }
func (n *Node) Bounded() bool  { return n.flags&nodeBounded != 0 }
func (n *Node) Addable() bool  { return n.flags&nodeAddable != 0 }
func (n *Node) HasCall() bool  { return n.flags&nodeHasCall != 0 }
func (n *Node) Likely() bool   { return n.flags&nodeLikely != 0 }
func (n *Node) HasVal() bool   { return n.flags&nodeHasVal != 0 }
func (n *Node) HasOpt() bool   { return n.flags&nodeHasOpt != 0 }
func (n *Node) Embedded() bool { return n.flags&nodeEmbedded != 0 }

@mvdan
Copy link
Member

mvdan commented Oct 19, 2017

However, I would agree that it makes little sense to allow no empty line between two funcs if either is multi-line.

@griesemer
Copy link
Contributor

This is working as intended. We went back and forth on this many times. The conclusion here is that gofmt respects the decision of the source author. Sometimes you have a situation with multiple one-liners where one of them, in the middle, spans multiple lines. We don't want to break that grouping; i.e., we want to keep all these functions together to express that they belong together.

There's lots of if's and when's etc. and there's just no good way to capture intent well, automatically. Better to let the author decide.

@golang golang locked and limited conversation to collaborators Oct 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

5 participants