A Go tool that sorts methods within types for better code readability. The tool analyzes call graphs and method usage patterns to optimize method ordering.
- Intelligent Method Sorting: Orders methods based on call depth and usage patterns
- Call Graph Analysis: Builds dependency graphs to identify entry points and helpers
- Multiple Integration Options: Standalone CLI tool + golangci-lint analyzer
- Configurable: Customize sorting criteria via configuration files
- Safe: Preserves code semantics while improving readability
Methods are sorted by the following criteria:
- Receiver Type: Methods are grouped by their receiver type (alphabetical)
- Exported First: Public methods appear before private methods
- Call Depth: Entry points (low depth) come before deep helpers
- In-Degree: Shared helpers (high in-degree) appear last
- Original Position: Stable sort fallback
This means:
- Public entry points appear at the top
- Deep internal helpers appear near the bottom
- Shared utility methods appear at the bottom
go install github.com/borovikovd/gomsort@latestDownload from the releases page.
git clone https://github.com/borovikovd/gomsort.git
cd gomsort
make build# Sort methods in a single file
gomsort file.go
# Sort methods in all Go files in current directory (recursive by default)
gomsort .
# Sort methods in a specific directory tree
gomsort ./src/
# Dry run to see what would be changed
gomsort -n file.go
# Verbose output
gomsort -v file.go-n: Dry run - show what would be changed without modifying files-v: Verbose output
Note: Like go fmt, gomsort processes directories recursively by default.
Add to your .golangci.yml:
linters:
enable:
- msort
linters-settings:
msort:
# Configuration options hereBefore:
type Server struct {
addr string
}
func (s *Server) helper() string {
return "help"
}
func (s *Server) Start() error {
return s.connect()
}
func (s *Server) connect() error {
s.helper()
return nil
}
func (s *Server) Stop() error {
return nil
}After:
type Server struct {
addr string
}
func (s *Server) Start() error {
return s.connect()
}
func (s *Server) Stop() error {
return nil
}
func (s *Server) connect() error {
s.helper()
return nil
}
func (s *Server) helper() string {
return "help"
}Create a .msort.json file in your project root:
{
"sort_criteria": {
"group_by_receiver": true,
"exported_first": true,
"sort_by_depth": true,
"sort_by_in_degree": true,
"preserve_original_order": true
},
"exclude": ["*_test.go"],
"include": ["*.go"]
}- Go 1.21 or later
- make (optional, for convenience)
make buildmake test
make test-coveragemake lint
make lint-fixmake dev # fmt + lint + test- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Run tests and linting (
make dev) - Commit your changes (
git commit -am 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
The tool performs the following analysis:
- Parse AST: Extract all method declarations and their receivers
- Build Call Graph: Analyze method calls to build dependency relationships
- Calculate Metrics:
- InDegree: Number of distinct methods that call this method
- MaxDepth: Longest call chain where this method appears
- Sort Methods: Apply sorting criteria to optimize readability
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by code organization principles from Clean Code and other software engineering best practices
- Built using Go's excellent AST and static analysis packages