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

go/format: go1.11 gofmt regression in map entries indent #27025

Closed
dolmen opened this issue Aug 16, 2018 · 2 comments
Closed

go/format: go1.11 gofmt regression in map entries indent #27025

dolmen opened this issue Aug 16, 2018 · 2 comments

Comments

@dolmen
Copy link
Contributor

dolmen commented Aug 16, 2018

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

gofmt from go1.11rc1

Does this issue reproduce with the latest release?

yes, go1.11rc1

What did you do?

main.go formatted with gofmt from go1.10:

package main

var x = map[string]bool{
	"aaaaaaaaaaaa":                             true, // <---
	"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": false,
	"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb":   true,
	"bbbbbbbbbbbb":                             false,
}
$(go1.11rc1 env GOROOT)/bin/gofmt -d main.go
diff -u main.go.orig main.go
--- main.go.orig	2018-08-16 12:52:52.329803591 +0200
+++ main.go	2018-08-16 12:52:52.330541553 +0200
@@ -1,7 +1,7 @@
 package main

 var x = map[string]bool{
-	"aaaaaaaaaaaa":                             true, // <---
+	"aaaaaaaaaaaa": true, // <---
 	"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": false,
 	"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb":   true,
 	"bbbbbbbbbbbb":                             false,

What did you expect to see?

No change in formatting of this code between go1.10 and go1.11

What did you see instead?

gofmt from go1.11rc1 reformats this code in a lesser readable way than go1.10: first entry of the map is not indented in the same way as other entries.

package main

var x = map[string]bool{
	"aaaaaaaaaaaa": true, // <---
	"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": false,
	"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb":   true,
	"bbbbbbbbbbbb":                             false,
}
@ALTree
Copy link
Member

ALTree commented Aug 16, 2018

This is expected. From the Go 1.11 release notes:

One minor detail of the default formatting of Go source code has changed. When formatting expression lists with inline comments, the comments were aligned according to a heuristic. However, in some cases the alignment would be split up too easily, or introduce too much whitespace. The heuristic has been changed to behave better for human-written code.

Also note that:

[...] these kinds of minor updates to gofmt are expected from time to time. In general, systems that need consistent formatting of Go source code should use a specific version of the gofmt binary.

@agnivade
Copy link
Contributor

Closing as this is working as intended. Please see #26228 for more details.

Thank you.

@golang golang locked and limited conversation to collaborators Aug 16, 2019
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

4 participants