Go tool that sorts methods by call graph analysis

1 dborovikov 1 9/6/2025, 1:37:40 PM pkg.go.dev ↗

Comments (1)

dborovikov · 1h ago
I got tired of scrolling through poorly organized Go methods, so I built gomsort - a tool that automatically sorts methods within types using call graph analysis.

The Problem: Methods in Go structs are often randomly ordered, making code hard to follow. You end up with public entry points scattered between private helpers.

The Solution: gomsort analyzes your call graphs and sorts methods by: 1. Public methods first 2. Entry points (low call depth) before helpers 3. Shared utilities (high in-degree) at the bottom

Example transformation:

  // Before - random order
  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 - logical flow
  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" }
Features: - Works like go fmt - recursive by default - Integrates with golangci-lint - Preserves comments and semantics - Configurable via .msort.json

Install: go install github.com/borovikovd/gomsort@latest

Repo: https://github.com/borovikovd/gomsort

What do you think? Would this help your codebases?