This is a proposal to add functionality to gofmt for formatting/alignment of multiple struct tags. If gofmt could align the tags it would become much more readable and save allot of time from having to do it manually.
example: ( just some tags I use, could be many others )
type User struct {
Name string `json:"name" bson:"name" validate:"required"`
LegalName string `json:"legalName" bson:"legalName" validate:"required"`
Email string `json:"email" bson:"email" validate:"required"`
....
}
after gofmt:
type User struct {
Name string `json:"name" bson:"name" validate:"required"`
LegalName string `json:"legalName" bson:"legalName" validate:"required"`
Email string `json:"email" bson:"email" validate:"required"`
...
}
gofmt doesn't usually change the contents of strings as it usually cannot know what they are used for (not semantics-preserving).
One might argue that this is different because gofmt knows that these are struct field tags and they follow a convention.
However, note that it's only a convention. Neither the spec nor the compiler enforce this convention. Any program is free to use the tags as it pleases; and the spacing may matter. In general, gofmt cannot do the right thing here.
One could add an option, but we have refrained from adding options to gofmt (in fact, we have removed as many as we can). Furthermore, even with an option, one would need to know that only tag strings following the convention occur in a program. That's not obvious unless you wrote the code in the first place.
I am against this proposal.
Although Neither the spec or compiler enforces this convention, as far as I know it is a requirement of how the reflect package accesses tag values and so I don't think that every program is free to use tags as it pleases because if a program is to use them, it has to adhere to this convention; unless there is another way to access tag values that I am unaware of.
I thought that gofmt knowing that these are struct field tags that must follow a convention would be enough to avoid causing any issues by only formatting/aligning tags that follow this convention.
@joeybloggs you can use reflect to access the tag as a normal string (type StructTag string) -- you don't need to use the Get/Lookup accessors. Indeed, the documentation says
By convention, tag strings are a concatenation of optionally space-separated key:"value" pairs.
(emphasis mine)
@griesemer aren't tabs vs spaces equally a convention not enforced by the spec or compiler?
I'll try to check the github dataset later to see if I can find any instances where spacing in struct tags is preserved for semantic meaning
I'll try to check the github dataset later to see if I can find any instances where spacing in struct tags is preserved for semantic meaning
Not finding such instance does not prove it doesn't exist elsewhere, eg. in a closed source code.
Gofmt has absolutely no business in modifying anyone's string literals. (There are exceptions which could be proven neutral but this proposal is not one of them.)
Given the strong opposition to this (including my own), I am declining聽this proposal. Thanks.
Most helpful comment
gofmt doesn't usually change the contents of strings as it usually cannot know what they are used for (not semantics-preserving).
One might argue that this is different because gofmt knows that these are struct field tags and they follow a convention.
However, note that it's only a convention. Neither the spec nor the compiler enforce this convention. Any program is free to use the tags as it pleases; and the spacing may matter. In general, gofmt cannot do the right thing here.
One could add an option, but we have refrained from adding options to gofmt (in fact, we have removed as many as we can). Furthermore, even with an option, one would need to know that only tag strings following the convention occur in a program. That's not obvious unless you wrote the code in the first place.
I am against this proposal.