You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
proposal: cmd/gofmt formatting/alignment of multiple struct tags #16918
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 )
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.
Reacted by Josh Bleecher Snyder, Ariel Mashraki, DeedleFake, Victor Vrantchan, Andrew Gerrand, Joe Tsai, siadat and Matt Kantor
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.
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.)
Activity
griesemer commentedon Aug 29, 2016
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.
deankarn commentedon Aug 29, 2016
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.
cespare commentedon Aug 29, 2016
@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(emphasis mine)
fortytw2 commentedon Aug 31, 2016
@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
cznic commentedon Aug 31, 2016
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.)
adg commentedon Oct 3, 2016
Given the strong opposition to this (including my own), I am declining this proposal. Thanks.