seriallink / formattag

Align Golang struct tags

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Formattag

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{}

Installation

Compile from source

Compile from source, which requires Go 1.18 or newer:

git clone github.com/momaek/formattag
cd formattag && go build .

Prebuild binaries

Please check the release page and download the lastest release.

Usage

formattag -file /path/to/your/golang/file

This command will change your go file.

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

VSCode

Please Install Run On Save Add this to settings.json commands:

{
    "match": "\\.go$",
    "isAsync": false,
    "cmd": "/path/to/formattag -file ${file}"
}

GoLand

e.g.

About

Align Golang struct tags


Languages

Language:Go 79.1%Language:Makefile 12.6%Language:Vim Script 8.3%