Question on gofmt and go vet and field tag keys

35 views
Skip to first unread message

Markus Heukelom

unread,
Jul 23, 2020, 4:28:54 AM7/23/20
to golang-nuts
Is there a way to instruct gofmt (or another tool) to reformat field tags such that the keys are aligned?

For example:

type Example struct {
Field1 string `json:"x" db:"y"`
Field2 int `json:"ababababc" db:"def"`

Field3 string `json:"zyx" db:"egh"`
Field4 string `json:"z" db:"q"`
}

Would be formatted as

type Example struct {
Field1 string  `json:"x"         db:"y"`
Field2 int     `json:"ababababc" db:"def"`

Field3 string `json:"zyx" db:"egh"`
Field4 string `json:"z"   db:"q"`
}

(View example in fixed font width please)

Is there a way to instruct go vet (or another tool) to report different field tag key order?

For example: 

type Example2 struct {
Field1 string `db:"y" json:"x"`
Field2 int `json:"abc" db:"def"`
}

Would output something along the lines of: "field2: different tag order for json and db"

Thanks.
Reply all
Reply to author
Forward
0 new messages