Apply tag to every field in every struct

Issue

Is it possible to apply a default struct field tag to every struct / many structs without having to type it out, and also keeps code cleaner.

Eg: when i need omitempty to be applied by default to every struct

type SomeStruct struct {
    FieldOne string `json:"field_one,omitempty"`
    FieldTwo string `json:"field_two,omitempty"`
    FieldThree string `json:"field_three,omitempty"`
}

Writing middleware for my API to parse all JSON responses, then remove empty fields seems very expensive.

Solution

Go tool to modify/update field tags in structs. gomodifytags makes it easy to update, add or delete the tags in a struct field. You can easily add new tags, update existing tags (such as appending a new key, i.e: db, xml, etc..) or remove existing tags. It also allows you to add and remove tag options. It supports atom,vscode,vim-go,acme.

https://github.com/fatih/gomodifytags

Answered By – ban

Answer Checked By – Timothy Miller (GoLangFix Admin)

Leave a Reply

Your email address will not be published.