Go: cmd/gofmt: address formatting oddities of composite literals with irregularly sized entries

Created on 22 Nov 2017  路  6Comments  路  Source: golang/go

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

1.9.2

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

Windows 10 (1709) 64-bit

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\matt\go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\matt\AppData\Local\Temp\go-build832937591=/tmp/go-build -gno-record-gcc-switches
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config

What did you do?

Created maps with long keys.
^ Hit the playground's format button to reproduce the issue

What did you expect to see?

Blocks formatted consistently

What did you see instead?

Broken Indentation blocks when running Go Fmt.
^ Hit the playground's format button to reproduce the issue

FrozenDueToAge NeedsFix early-in-cycle

Most helpful comment

Looking at this again, I admit that the formatting looks pretty ugly in this case. Also, given that this very issue has come up so many times, it may be worthwhile re-investigating the formatting of literals of this form. There's a few solutions I see:

  • don't do anything (doesn't address the issue) [trivial]
  • improve the heuristics (e.g., don't change alignment if there's only a few shorter entries) [hard]
  • align all entries, and leave it to programmer to introduce empty lines to break alignment [easy]

All 6 comments

This is by design. Search the issue tracker where @griesemer has explained it a few times.

In general, the best strategy to being happy about gofmt's choices is to not worry about it.

Maybe this needs a FAQ entry. I'll let @griesemer decide whether to keep this bug open as a documentation bug.

Fine to leave this open as documentation issue.

Looking at this again, I admit that the formatting looks pretty ugly in this case. Also, given that this very issue has come up so many times, it may be worthwhile re-investigating the formatting of literals of this form. There's a few solutions I see:

  • don't do anything (doesn't address the issue) [trivial]
  • improve the heuristics (e.g., don't change alignment if there's only a few shorter entries) [hard]
  • align all entries, and leave it to programmer to introduce empty lines to break alignment [easy]

In my opinion, the align all option would be easier for programmers to reason with and understand. 馃憤

Change https://golang.org/cl/104755 mentions this issue: go/printer, gofmt: tuned table alignment for better results

Change https://golang.org/cl/104836 mentions this issue: cmd/coordinator: remove gofmt inconsistency

Was this page helpful?
0 / 5 - 0 ratings