Gomodifytags
Go tool to modify struct field tags
Install / Use
/learn @fatih/GomodifytagsREADME
gomodifytags 
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's
intended to be used by an editor, but also has modes to run it from the
terminal. Read the usage section below for more information.

Install
go install github.com/fatih/gomodifytags@latest
Supported editors
- vim-go with
:GoAddTagsand:GoRemoveTags - go-plus (atom) with commands
golang:add-tagsandgolang:remove-tags - vscode-go with commands
Go: Add TagsandGo: Remove Tags - A (Acme) with commands
addtagsandrmtags - emacs-go-tag with commands
go-tag-addandgo-tag-remove - TextMate2
Usage
gomodifytags has multiple ways to modify a tag. Let's start with an example package:
package main
type Server struct {
Name string
Port int
EnableLogs bool
BaseDomain string
Credentials struct {
Username string
Password string
}
}
We have to first pass a file. For that we can use the -file flag:
$ gomodifytags -file demo.go
-line, -offset, -struct or -all is not passed
What are these? There are four different ways of defining which field tags to change:
-struct: This accepts the struct name. i.e:-struct Server. The name should be a valid type name. The-structflag selects the whole struct, and thus it will operate on all fields.-field: This accepts a field name. i.e:-field Address. Useful to select a certain field. The name should be a valid field name. The-structflag is required.-offset: This accepts a byte offset of the file. Useful for editors to pass the position under the cursor. i.e:-offset 548. The offset has to be inside a valid struct. The-offsetselects the whole struct. If you need more granular option see-line-line: This accepts a string that defines the line or lines of which fields should be changed. I.e:-line 4or-line 5,8-all: This is a boolean. The-allflag selects all structs of the given file.
Let's continue by using the -struct tag:
$ gomodifytags -file demo.go -struct Server
one of [-add-tags, -add-options, -remove-tags, -remove-options, -clear-tags, -clear-options] should be defined
Adding tags & options
There are many options on how you can change the struct. Let us start by adding
tags. The following will add the json key to all fields. The value will be
automatically inherited from the field name and transformed to snake_case:
$ gomodifytags -file demo.go -struct Server -add-tags json
package main
type Server struct {
Name string `json:"name"`
Port int `json:"port"`
EnableLogs bool `json:"enable_logs"`
BaseDomain string `json:"base_domain"`
Credentials struct {
Username string `json:"username"`
Password string `json:"password"`
} `json:"credentials"`
}
By default changes will be printed to stdout and can be used for dry-run your
changes before making destructive changes. If you want to change it permanently,
pass the -w (write) flag.
$ gomodifytags -file demo.go -struct Server -add-tags json -w
You can disable printing the results to stdout with the --quiet flag:
$ gomodifytags -file demo.go -struct Server -add-tags json -w --quiet
You can pass multiple keys to add tags. The following will add json and xml
keys:
$ gomodifytags -file demo.go -struct Server -add-tags json,xml
package main
type Server struct {
Name string `json:"name" xml:"name"`
Port int `json:"port" xml:"port"`
EnableLogs bool `json:"enable_logs" xml:"enable_logs"`
BaseDomain string `json:"base_domain" xml:"base_domain"`
Credentials struct {
Username string `json:"username" xml:"username"`
Password string `json:"password" xml:"password"`
} `json:"credentials" xml:"credentials"`
}
The default transformation is snake_case when using the gomodifytags command
and when calling Apply() - see section below for more information about Apply().
If you prefer to use camelCase instead of snake_case for the values, you
can use the -transform flag to define a different transformation rule. The
following example uses the camelcase transformation rule:
$ gomodifytags -file demo.go -struct Server -add-tags json,xml -transform camelcase
package main
type Server struct {
Name string `json:"name" xml:"name"`
Port int `json:"port" xml:"port"`
EnableLogs bool `json:"enableLogs" xml:"enableLogs"`
BaseDomain string `json:"baseDomain" xml:"baseDomain"`
Credentials struct {
Username string `json:"username" xml:"username"`
Password string `json:"password" xml:"password"`
} `json:"credentials" xml:"credentials"`
}
Formatting tag values
By default a struct tag's value is transformed from a struct's field and used
directly. As an example for the field Server string, we generate a tag in the
form: json:"server" (assuming -add-tags=json is used).
However, some third party libraries use tags in a different way and might
require to them to have a particular formatting, such as is the case of
prefixing them (field_name=<your_value>). The --template flag allows you to
specify a custom format for the tag value to be applied.
$ gomodifytags -file demo.go -struct Server -add-tags gaum -template "field_name={field}"
package main
type Server struct {
Name string `gaum:"field_name=name"`
Port int `gaum:"field_name=port"`
EnableLogs bool `gaum:"field_name=enableLogs"`
BaseDomain string `gaum:"field_name=baseDomain"`
}
The {field} word is a special keyword that is replaced by the struct tag's value
after the transformation.
Transformations
We currently support the following transformations:
snakecase:"BaseDomain"->"base_domain"camelcase:"BaseDomain"->"baseDomain"lispcase:"BaseDomain"->"base-domain"pascalcase:"BaseDomain"->"BaseDomain"titlecase:"BaseDomain"->"Base Domain"keep: keeps the original field name
You can also pass a static value for each fields. This is useful if you use Go
packages that validates the struct fields or extract values for certain
operations. The following example adds the json key, a validate key with
the value set to gt=1 and the scope key with the value read-only:
$ gomodifytags -file demo.go -struct Server -add-tags json,validate:gt=1,scope:read-only
package main
type Server struct {
Name string `json:"name" validate:"gt=1" scope:"read-only"`
Port int `json:"port" validate:"gt=1" scope:"read-only"`
EnableLogs bool `json:"enable_logs" validate:"gt=1" scope:"read-only"`
BaseDomain string `json:"base_domain" validate:"gt=1" scope:"read-only"`
Credentials struct {
Username string `json:"username" validate:"gt=1" scope:"read-only"`
Password string `json:"password" validate:"gt=1" scope:"read-only"`
} `json:"credentials" validate:"gt=1" scope:"read-only"`
}
To add options to for a given key, we use the -add-options flag. In the
example below we're going to add the json key and the omitempty option to
all json keys:
$ gomodifytags -file demo.go -struct Server -add-tags json -add-options json=omitempty
package main
type Server struct {
Name string `json:"name,omitempty"`
Port int `json:"port,omitempty"`
EnableLogs bool `json:"enable_logs,omitempty"`
BaseDomain string `json:"base_domain,omitempty"`
Credentials struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
} `json:"credentials,omitempty"`
}
If the key already exists you don't have to use -add-tags
Skipping unexported fields
By default all fields are processed. This main reason for this is to allow
structs to evolve with time and be ready in case a field is exported in the
future. However if you don't like this behavior, you can skip it by passing the
--skip-unexported flag:
$ gomodifytags -file demo.go -struct Server -add-tags json --skip-unexported
package main
type Server struct {
Name string `json:"name"`
Port int `json:"port"`
enableLogs bool
baseDomain string
}
Removing tags & options
Let's continue with removing tags. We're going to use the following simple package:
package main
type Server struct {
Name string `json:"name,omitempty" xml:"name,attr,cdata"`
Port int `json:"port,omitempty" xml:"port,attr,cdata"`
EnableLogs bool `json:"enable_logs,omitempty" xml:"enable_logs,attr,cdata"`
BaseDomain string `json:"base_domain,omitempty" xml:"base_domain,attr,cdata"`
Credentials struct {
Username string `json:"username,omitempty" xml:"username,attr,cdata"`
Password string `json:"password,omitempty" xml:"password,attr,cdata"`
} `json:"credentials,omitempty" xml:"credentials,attr,cdata"`
}
To remove the xml tags, we're going to use the -remove-tags flag:
$ gomodifytags -file demo.go -struct Server -remove-tags xml
package main
type Server struct {
Name string `json:"name"`
Port int `json:"port"`
