The tool is used to align golang struct's tags.
eg.:
Before
// TestStruct this is a test struct
type TestStruct struct {
ID string `json:"id" xml:"id"`
IfNotModified string `json:"if_not_modified" xml:"if_not_modified"`
Name string `json:"name" xml:"name"`
ThisIsAStructWodeTianNa struct {
FieldFromThisIsAStructWodeTianNa string `json:"field_from_this_is_a_struct_wode_tian_na" xml:"field_from_this_is_a_struct_wode_tian_na"`
TianName string `json:"tian_name" xml:"tian_name"`
} `json:"this_is_a_struct_wode_tian_na" xml:"this_is_a_struct_wode_tian_na"`
T time.Time `json:"t" xml:"t"`
Fset Fset `json:"fset" xml:"fset"`
}
type Fset struct{}
After
// TestStruct this is a test struct
type TestStruct struct {
ID string `json:"id" xml:"id"`
IfNotModified string `json:"if_not_modified" xml:"if_not_modified"`
Name string `json:"name" xml:"name"`
ThisIsAStructWodeTianNa struct {
FieldFromThisIsAStructWodeTianNa string `json:"field_from_this_is_a_struct_wode_tian_na" xml:"field_from_this_is_a_struct_wode_tian_na"`
TianName string `json:"tian_name" xml:"tian_name"`
} `json:"this_is_a_struct_wode_tian_na" xml:"this_is_a_struct_wode_tian_na"`
T time.Time `json:"t" xml:"t"`
Fset Fset `json:"fset" xml:"fset"`
}
type Fset struct{}
go install github.com/momaek/formattag@latest
Compile from source, which requires Go 1.18 or newer:
git clone github.com/momaek/formattag
cd formattag && go build .
Please check the release page and download the lastest release.
formattag -file /path/to/your/golang/file
This command will change your go file.
If you want print result on console:
formattag -file /path/to/your/golang/file -C
You can also get input from stdin(result will be print on console):
cat /path/to/your/golang/file | formattag
If you're using vim or nvim, you'd better install from source.
Add the following snippet to your ~/.vimrc:
set rtp+={/path/to/your/downloaded/source}/formattag/vim
Running :PrettyTag
will run formattag on the current file.
Optionally, add this to your ~/.vimrc
to automatically run formattag
on :w
autocmd BufWritePost,FileWritePost *.go execute 'PrettyTag' | checktime
Please check https://github.com/M1ndo/gofmt-tag.
Provided by M1ndo
Please Install Run On Save
Add the following code snippet to settings.json
.
{
"match": "\\.go$",
"isAsync": false,
"cmd": "/path/to/formattag -file ${file}"
}
e.g.