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

Comments

@qinhanlei
Copy link

@qinhanlei qinhanlei commented Oct 19, 2017

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
Copy link
Member

@ALTree ALTree commented Oct 19, 2017

@mvdan
Copy link
Member

@mvdan 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 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

@griesemer griesemer commented Oct 19, 2017

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.

@griesemer griesemer closed this Oct 19, 2017
@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.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.