https://github.com/momaek/formattag
Align Golang struct tags
https://github.com/momaek/formattag
align align-struct-tag golang pretty structtags
Last synced: 4 months ago
JSON representation
Align Golang struct tags
- Host: GitHub
- URL: https://github.com/momaek/formattag
- Owner: momaek
- Created: 2021-04-16T07:04:24.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-30T15:24:33.000Z (almost 2 years ago)
- Last Synced: 2024-07-03T11:46:05.448Z (almost 2 years ago)
- Topics: align, align-struct-tag, golang, pretty, structtags
- Language: Go
- Homepage:
- Size: 267 KB
- Stars: 33
- Watchers: 1
- Forks: 6
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Formattag
The tool is used to align golang struct's tags.
eg.:
Before
```golang
// 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
```golang
// 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{}
```
### Installation
#### Using go install
```
go install github.com/momaek/formattag@latest
```
#### Compile from source
Compile from source, which requires [Go 1.18 or newer](https://golang.org/doc/install):
```
git clone github.com/momaek/formattag
cd formattag && go build .
```
#### Prebuild binaries
Please check the [release page](https://github.com/momaek/formattag/releases) and download the lastest release.
### Usage
```
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
```
#### Vim
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
```
#### Emacs
Please check [https://github.com/M1ndo/gofmt-tag](https://github.com/M1ndo/gofmt-tag).
Provided by [M1ndo](https://github.com/M1ndo)
#### VSCode
Please Install [Run On Save](https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave)
Add the following code snippet to `settings.json`.
```json
{
"match": "\\.go$",
"isAsync": false,
"cmd": "/path/to/formattag -file ${file}"
}
```
#### GoLand
e.g.
