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
Comments
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"`
} |
This is all working as expected. Fields are aligned per group. Comments, newlines or significantly different length identifiers all introduce new groups. |
@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. |
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. |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?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:
So, i tried to do the following:
What did you expect to see?
Here's what i was expecting to get?
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.The text was updated successfully, but these errors were encountered: