Skip to content

proposal: cmd/gofmt formatting/alignment of multiple struct tags #16918

Closed

Description

@deankarn

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"`
        ...
}

Activity

  1. griesemer commented on Aug 29, 2016

    @griesemer
    Contributor

    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.

  2. deankarn commented on Aug 29, 2016

    @deankarn
    Author

    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.

  3. cespare commented on Aug 29, 2016

    @cespare
    Contributor

    @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)

  4. fortytw2 commented on Aug 31, 2016

    @fortytw2

    @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

  5. cznic commented on Aug 31, 2016

    @cznic
    Contributor

    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.)

  6. added this to the Proposal milestone on Sep 6, 2016
  7. adg commented on Oct 3, 2016

    @adg
    Contributor

    Given the strong opposition to this (including my own), I am declining this proposal. Thanks.

  8. locked and limited conversation to collaborators on Oct 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions