Can we have some sort of build flag at compile time to apply `fieldalignment` on each struct in our project?

I’m using golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment for field alignment and I’m having very bad moments reading my structs because I have this:

type Player struct {
  createdAt time.Time
  team      *Team
  pumID     *uint64
  Sponsorship
  description string
  teamID      uint64
  projectID   uint64
  categoryID  uint64
  authorID    uint64
  id          uint64
}

instead for example of this:

type Player struct {
  id        uint64
  createdAt time.Time

  description string

  authorID   uint64
  categoryID uint64
  projectID  uint64
  pumID      *uint64

  teamID uint64
  team   *Team

  Sponsorship
}

which is faster and clearer to read and understand for me.

PROPOSAL

Can we have some sort of build flag at compile time to apply fieldalignment on each struct in our project?

Something like:

go build --fieldalignment .

It would be very welcome and simple to realize I think.

Sorry if I wrote the proposal wrong.

You can use GitHub - orijtech/structslop: structslop is a static analyzer for Go that recommends struct field rearrangements to provide for maximum space/allocation efficiency. library with -apply flag.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.