Align Golang struct tags

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, which requires Go 1.15 or newer:

go get github.com/momaek/formattag

Usage

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

This command will change your go file.

Vim

Add this to your ~/.vimrc:

set rtp+=$GOPATH/src/github.com/momaek/formattag/vim

If you have multiple entries in your GOPATH, replace $GOPATH with the right value.

If you're using go mod, replace $GOPATH with $GOMODCACH:

set rtp+=$GOMODCACHE/github.com/momaek/formattag@{version}/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}"
}
Owner
Similar Resources

Golang-echo-sample - Make an out-of-the-box backend based on golang-echo

Golang-echo-sample - Make an out-of-the-box backend based on golang-echo

Dec 31, 2021

Minimalistic, pluggable Golang evloop/timer handler with dependency-injection

Anagent Minimalistic, pluggable Golang evloop/timer handler with dependency-injection - based on codegangsta/inject - go-macaron/inject and chuckpresl

Sep 27, 2022

GoLang Library for Browser Capabilities Project

Browser Capabilities GoLang Project PHP has get_browser() function which tells what the user's browser is capable of. You can check original documenta

Sep 27, 2022

Golang counters for readers/writers

Datacounter Golang counters for readers/writers. Examples ReaderCounter buf := bytes.Buffer{} buf.Write(data) counter := datacounter.NewReaderCounter(

Oct 9, 2022

Golang beautify data display for Humans

Golang beautify data display for Humans English 简体中文 Install # Stable version go get -u -v gopkg.in/ffmt.v1 # Latest version go get -u -v github.com/

Dec 22, 2022

a generic object pool for golang

Go Commons Pool The Go Commons Pool is a generic object pool for Golang, direct rewrite from Apache Commons Pool. Features Support custom PooledObject

Jan 5, 2023

Resiliency patterns for golang

go-resiliency Resiliency patterns for golang. Based in part on Hystrix, Semian, and others. Currently implemented patterns include: circuit-breaker (i

Jan 3, 2023

psutil for golang

gopsutil: psutil for golang This is a port of psutil (https://github.com/giampaolo/psutil). The challenge is porting all psutil functions on some arch

Jan 2, 2023

Type-safe Prometheus metrics builder library for golang

gotoprom A Prometheus metrics builder gotoprom offers an easy to use declarative API with type-safe labels for building and using Prometheus metrics.

Dec 5, 2022
Comments
  • Oops the formattag remove tag

    Oops the formattag remove tag

    The original content:

    package models
    
    import (
    	"gorm.io/gorm"
    )
    
    // Location 位置,分部,办公室
    type Location struct {
    	ID             uint    `json:"id,omitempty"    gorm:"primaryKey"`                                           // 主建
    	OrganizationID uint    `json:"organization_id" gorm:"uniqueIndex:uix_org_name"`                             // 组织 ID
    	Name           string  `json:"name"            gorm:"size:255;uniqueIndex:uix_org_name" binding:"required"` // 名称
    	Description    string  `json:"description"     gorm:"type:text"`                                            // 描述
    	Avatar         string  `json:"avatar"`                                                                      // LOGO
    	Address        string  `json:"address"`                                                                     // 地址
    	ZipCode        string  `json:"zip_code"`                                                                    // 邮政编码
    	Phone          string  `json:"phone"`                                                                       // 电话号码
    	Fax            string  `json:"fax"`                                                                         // 传真
    	Position       float64 `json:"position"`                                                                    // 排序位置
    
    	CreatedAt int64 `json:"created_at,omitempty"` // 创建时间
    	UpdatedAt int64 `json:"updated_at,omitempty"` // 更新时间
    
    	CountMembers int `json:"count_members" sql:"-"`
    }
    
    // AfterCreate gorm after create Callback
    func (location *Location) AfterCreate(tx *gorm.DB) (err error) {
    	err = tx.UpdateColumn("position", location.ID).Error
    	return
    }
    

    After run formattag:

    package models
    
    import (
    	"gorm.io/gorm"
    )
    
    // Location 位置,分部,办公室
    type Location struct {
    	ID             uint    `json:"id,omitempty"    gorm:"primaryKey"`                                           // 主建
    	OrganizationID uint    `json:"organization_id" gorm:"uniqueIndex:uix_org_name"`                             // 组织 ID
    	Name           string  `json:"name"            gorm:"size:255;uniqueIndex:uix_org_name" binding:"required"` // 名称
    	Description    string  `json:"description"     gorm:"type:text"`                                            // 描述
    	Avatar         string  `json:"avatar"`                                                                      // LOGO
    	Address        string  `json:"address"`                                                                     // 地址
    	ZipCode        string  `json:"zip_code"`                                                                    // 邮政编码
    	Phone          string  `json:"phone"`                                                                       // 电话号码
    	Fax            string  `json:"fax"`                                                                         // 传真
    	Position       float64 `json:"position"`                                                                    // 排序位置
    
    	CreatedAt int64 `json:"created_at,omitempty"` // 创建时间
    	UpdatedAt int64 // 更新时间
    
    	CountMembers int `json:"count_members" sql:"-"`
    }
    
    // AfterCreate gorm after create Callback
    func (location *Location) AfterCreate(tx *gorm.DB) (err error) {
    	err = tx.UpdateColumn("position", location.ID).Error
    	return
    }
    
    
  • Fixed len count on multi-byte characters

    Fixed len count on multi-byte characters

    Before fix

    type Diacritics struct {
        A string `foo:"ä" json:"a"`
        E string `foo:"e"  json:"e"`
        I string `foo:"ï" json:"i"`
        O string `foo:"o"  json:"o"`
        U string `foo:"ü" json:"u"`
    }
    

    After fix

    type Diacritics struct {
        A string `foo:"ä" json:"a"`
        E string `foo:"e" json:"e"`
        I string `foo:"ï" json:"i"`
        O string `foo:"o" json:"o"`
        U string `foo:"ü" json:"u"`
    }
    
Stackoverflow-Tag-Recommender - Calculate similarity of different tags based on the data of stackoverflow

Recommender System with Stackoverflow's Data This project is written to recommen

Apr 4, 2022
An ease to use finit state machine golang implementation.Turn any struct to a fsm with graphviz visualization supported.

go-fsm An ease to use finit state machine golang implementation.Turn any struct to a fsm with graphviz visualization supported. usage import github.co

Dec 26, 2021
Random fake data and struct generator for Go.

Faker Random fake data and struct generator for Go. More than 100 generator functions Struct generator Unique data generator Builtin types support Eas

Oct 3, 2022
Provide Go Statistics Handler, Struct, Measure Method

Go Statistics Handler About The gosh is an abbreviation for Go Statistics Handler. This Repository is provided following functions. Go runtime statist

Jan 8, 2023
Gountries provides: Countries (ISO-3166-1), Country Subdivisions(ISO-3166-2), Currencies (ISO 4217), Geo Coordinates(ISO-6709) as well as translations, country borders and other stuff exposed as struct data.

gountries Inspired by the countries gem for ruby. Countries (ISO-3166-1), Country Subdivisions(ISO-3166-2), Currencies (ISO 4217), Geo Coordinates(ISO

Dec 22, 2022
RTS: request to struct. Generates Go structs from JSON server responses.

RTS: Request to Struct Generate Go structs definitions from JSON server responses. RTS defines type names using the specified lines in the route file

Dec 7, 2022
The kprobe package allows construction of dynamic struct based on kprobe event format descriptions.

The kprobe package allows construction of dynamic struct based on kprobe event format descriptions.

Oct 27, 2021
Golang CS:GO external base. Development currently halted due to compiler/runtime Golang bugs.

gogo Golang CS:GO External cheat/base. Also, my first Golang project. Wait! Development momentarily halted due to compiler/runtime bugs. Disclaimer Th

Jun 25, 2022
Belajar Golang Install Golang

Golang belajar Golang Install Golang = download di https://golang.org/dl/ = pilih yg Zip = extract file zipnya = buka foldernya - copy folder go = pas

Nov 15, 2021
Golang-module-references - A reference for how to setup a Golang project with modules - Task Management + Math Examples

Golang Module Project The purpose of this project is to act as a reference for setting up future Golang projects using modules. This project has a mat

Jan 2, 2022