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: inconsistent whitespace after map value type #53001

Open
dsnet opened this issue May 19, 2022 · 4 comments
Open

cmd/gofmt: inconsistent whitespace after map value type #53001

dsnet opened this issue May 19, 2022 · 4 comments
Labels
NeedsInvestigation
Milestone

Comments

@dsnet
Copy link
Member

@dsnet dsnet commented May 19, 2022

Consider the following:

var _ = map[string]func() bool{
	"Func": func() bool {
		return false
	},
}

On one line it's go fmt'd as func() bool{ and as func() bool { on the other line. Note the space between bool and {.

In the previous case, this is the syntax for a map literal, while the latter is a function declaration.

Perhaps we should always output a space after a type expression (e.g., func() bool or chan bool) that contains a space?

@mknyszek
Copy link
Contributor

@mknyszek mknyszek commented May 19, 2022

CC @griesemer

@mknyszek mknyszek added the NeedsInvestigation label May 19, 2022
@mknyszek mknyszek added this to the Backlog milestone May 19, 2022
@mknyszek
Copy link
Contributor

@mknyszek mknyszek commented May 19, 2022

Is this new at tip or has this behavior been present in Go 1.18 and earlier?

@dsnet
Copy link
Member Author

@dsnet dsnet commented May 19, 2022

I believe this has been the behavior since forever.

Given that Go1.19 is going to cause lots of code churn anyways (due to godoc formatting). This might be the best time to fix all the little go fmt quirks.

@cespare
Copy link
Contributor

@cespare cespare commented May 19, 2022

Personally I like it better how it is now.

The change would increase consistency in a textual sense by making the two strings the same (func() bool {) but syntactically it's less consistent since how map (and slice, array, ...) literals are formatted depends on the spelling of the type.

With the way it is today, when one's eyes see the space (or not) it's an immediate clue about whether you're reading, for example, a map literal or a function literal. And that is a tiny boon to readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation
Projects
None yet
Development

No branches or pull requests

3 participants