gosortstructs

command module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2024 License: MIT Imports: 14 Imported by: 0

README

gosortstructs

sorts structs - forked from danishprakash/gosortstructs to add support for nested anonymous structs

What

A command line tool which uses AST to sort fields of a Go struct for easier readability and better diffs. This tool is meant to be used as an underlying tool for text editors (vim-plugin). The resultant Go code conforms with gofmt but is eventually opinionated.

How
  • By default, this program alphabetically sorts the struct(s) in the specified file.
  • If an anonymous field is part of the struct, they are separately sorted and grouped at the end of the struct.
type Hotel struct {
   *Founder
   Rating   int
   Location string
   *Organization

}

to

type Hotel struct {
	Location       string
	Rating         int
	*Founder
	*Organization
}
Advantages
  • In large codebases, it makes it easier to identify all new fields that are added to a struct if they are sorted alphabetically.
  • If you've intermixed fields as in the above example, your struct tags would be wrongly indented which hampers readability.
Installation
go install github.com/meblum/gosortstructs@latest
Usage
$ gosortstructs --help
Usage of gosortstructs:
  -file string
        file name to be processed
  -reverse
        reverse alphabetical sort
  -struct string
        struct to sort
  -write
        write result to source file (overwrite)
  -line
        position of the struct/cursor (to be used programmatically)

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL