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

Gofmt can't format the structure if has comments between attributes #23999

Closed
denismakogon opened this issue Feb 21, 2018 · 4 comments
Closed

Comments

@denismakogon
Copy link

Please answer these questions before submitting your issue. Thanks!

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

go version go1.10 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/denismakogon/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH=---filtered----
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/lh/wxmjgcb14s37p3kq71l4bfrh0000gn/T/go-build627174261=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Iinter (https://github.com/alecthomas/gometalinter) was complaining about one of the struct definitions formattings is totally messed up. Here's the struct definition:

type API struct {
	ID           string `json:"id,omitempty"`
	Name         string `json:"name"`
	CreatedAt    int64  `json:"-"`
	UpstreamURL  string `json:"upstream_url,omitempty"`
	PreserveHost bool   `json:"preserve_host"`
	// kong 0.9.x and earlier
	RequestPath      string `json:"request_path,omitempty"`
	RequestHost      string `json:"request_host,omitempty"`
	StripRequestPath bool   `json:"strip_request_path,omitempty"`
	// kong 0.10.x and later
	Hosts    []string `json:"hosts,omitempty"`
	URIs     []string `json:"uris,omitempty"`
	StripURI bool     `json:"strip_uri,omitempty"`
}

So, i tried to do the following:

go fmt ./...

What did you expect to see?

Here's what i was expecting to get?

type API struct {
	ID               string   `json:"id,omitempty"`
	Name             string   `json:"name"`
	CreatedAt        int64    `json:"-"`
	UpstreamURL      string   `json:"upstream_url,omitempty"`
	PreserveHost     bool     `json:"preserve_host"`
	// kong 0.9.x and earlier
        RequestPath      string   `json:"request_path,omitempty"`
	RequestHost      string   `json:"request_host,omitempty"`
	StripRequestPath bool     `json:"strip_request_path,omitempty"`
	// kong 0.10.x and later
        Hosts            []string `json:"hosts,omitempty"`
	URIs             []string `json:"uris,omitempty"`
	StripURI         bool     `json:"strip_uri,omitempty"`
}

What did you see instead?

After running go fmt ./... no changes were applied to the given struct. I assume that comments make fmt faulty somehow.

@ericmdantas
Copy link

The problem doesn't happen exclusively to comments. If you simply remove the comments and leave the spacing there, the properties won't be formatted either. Like this:

type API struct {
	ID           string `json:"id,omitempty"`
	Name         string `json:"name"`
	CreatedAt    int64  `json:"-"`
	UpstreamURL  string `json:"upstream_url,omitempty"`
	PreserveHost bool   `json:"preserve_host"`

	RequestPath      string `json:"request_path,omitempty"`
	RequestHost      string `json:"request_host,omitempty"`
	StripRequestPath bool   `json:"strip_request_path,omitempty"`

	Hosts    []string `json:"hosts,omitempty"`
	URIs     []string `json:"uris,omitempty"`
	StripURI bool     `json:"strip_uri,omitempty"`
}

Go playground.

@dominikh
Copy link
Member

This is all working as expected. Fields are aligned per group. Comments, newlines or significantly different length identifiers all introduce new groups.

@denismakogon
Copy link
Author

@dominikh excuse, but how is that expected? Is it documented? As far as i know, it is not documented or expected. No matter what you put inside the struct (no matter would that be comments, newlines, etc.), it should be formatted.

@dominikh
Copy link
Member

It is formatted. You will notice that there are 3 distinct groups of fields. In each group, all fields are aligned. This has been covered before in #5277 and #10392

The style of gofmt is not documented explicitly; the implementation of gofmt defines the style. Sans bugs, what gofmt produces is the expected formatting, and this particular case isn't a bug.

@golang golang locked and limited conversation to collaborators Feb 21, 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