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.
go install github.com/fatih/gomodifytags@latest
- vim-go with
:GoAddTags
and:GoRemoveTags
- go-plus (atom) with commands
golang:add-tags
andgolang:remove-tags
- vscode-go with commands
Go: Add Tags
andGo: Remove Tags
- A (Acme) with commands
addtags
andrmtags
- emacs-go-tag with commands
go-tag-add
andgo-tag-remove
- TextMate2
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-struct
flag 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-struct
flag 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-offset
selects 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 4
or-line 5,8
-all
: This is a boolean. The-all
flag 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
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"`
}
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"`
}