README
Go Scope Extractor
Architecture-aware Go code extraction and visualization tool for understanding dependencies, scope, and hexagonal patterns.
Extract Go code with its dependencies, discover interfaceβimplementation mappings, detect DI frameworks, and visualize your architecture.
Key Features
Phase 1 & 2 (Core + Visualizer)
- Review-Focused: Extracts readable code with functional context
- Depth-Limited Extraction: Control dependency traversal depth (0=target only, 1=direct, 2=transitive, etc.)
- Multiple Output Formats: Markdown, JSON, HTML
- Smart Dependency Resolution: BFS traversal for systematic gathering
- Interactive Web Visualizer: D3.js force-directed graph with zoom, drag, and code viewing
Phase 3 (Architecture Analysis) π
- Interface Detection: Automatically discovers interfaces implemented by structs
- DI Framework Detection: Recognizes Wire, Fx, and manual DI patterns
- Hexagonal Architecture: Visual identification of ports, adapters, and factories
- Semantic Visualization: Color-coded nodes for interfaces (green), implementations (purple), constructors (orange)
Installation
CLI Tool
# Build the extraction tool
go build -o bin/go-scope ./cmd/go-scope
# Or install globally
go install ./cmd/go-scope
Visualizer Server
# Build the web server
go build -o bin/serve ./cmd/serve
# Or use any static file server
python3 -m http.server 8080 -d web/public
npx http-server web/public -p 8080
Usage
CLI Extraction
# Extract a function with direct dependencies (Markdown)
go-scope -file=pkg/math/add.go -line=42 -depth=1
# Extract as JSON for visualizer
go-scope -file=pkg/math/add.go -line=42 -depth=2 -format=json -output=extract.json
# Extract target only (no dependencies)
go-scope -file=pkg/math/add.go -line=42 -depth=0
# Save output to file
go-scope -file=pkg/math/add.go -line=42 -output=extract.md
# Verbose mode
go-scope -file=pkg/math/add.go -line=42 -verbose
Web Visualizer
# 1. Generate JSON extract
cd your-go-project
go-scope -file=pkg/math/add.go -line=42 -depth=2 -format=json -output=extract.json
# 2. Start visualizer server
./bin/serve web/public
# Or: python3 -m http.server 8080 -d web/public
# 3. Open http://localhost:8080 in browser
# 4. Click "Load Extract JSON" and select extract.json
# 5. Explore interactively!
Command Line Options
-file string
Source file to extract from (required)
-line int
Line number of target symbol (required)
-col int
Column number (default: 1)
-depth int
Dependency depth (0=target only, 1=direct deps, etc) (default: 1)
-format string
Output format: markdown, json, html (default: "markdown")
-output string
Output file (default: stdout)
-verbose
Show verbose output
Example
Given this code:
// pkg/math/add.go
func Add(a, b int) int {
if !validateInputs(a, b) {
fmt.Println("invalid inputs")
return 0
}
return a + b
}
// pkg/math/util.go
func validateInputs(a, b int) bool {
return a >= 0 && b >= 0
}Running:
cd examples/ex1
../../bin/go-scope -file=pkg/math/add.go -line=7 -depth=1Produces a markdown extract with:
- The
Addfunction code and documentation - The
validateInputsdependency code - External references to
fmt.Println - Metadata and location information
Architecture
The tool follows a clean architecture with three main phases:
internal/extract/locator.go)golang.org/x/tools/go/packages
- Finds target symbol at specified file:line:column
- Extracts symbol information (name, kind, code, documentation)internal/extract/collector.go)internal/extract/format/)Development
Built using Test-Driven Development (TDD) with comprehensive test coverage:
# Run tests
go test ./...
# Run tests with coverage
go test ./... -cover
# Run specific package tests
go test ./internal/extract -v
go test ./internal/extract/format -vTest coverage:
- Symbol locator: 75.3%
- Dependency collector: 75.3%
- Markdown formatter: 78.8%
Project Structure
.
βββ cmd/
β βββ go-scope/ # CLI entry point
β βββ main.go
βββ internal/
β βββ extract/ # Core extraction logic
β β βββ locator.go # Symbol location
β β βββ collector.go # Dependency collection
β β βββ api.go # Public API
β β βββ helpers.go # Internal types
β β βββ format/ # Output formatters
β β βββ markdown.go
β βββ types/ # Shared type definitions
β βββ types.go
βββ examples/
β βββ ex1/ # Example Go project for testing
β βββ pkg/math/
β βββ add.go
β βββ util.go
βββ docs/ # Documentation
β βββ SPEC_v2_REVIEW_FOCUSED.md
β βββ QUICK_START.md
β βββ ...
βββ bin/ # Built binaries
βββ go-scope
Design Principles
Future Enhancements
Phase 2 β Complete:
- [x] Interactive web visualizer
- [x] JSON output format
- [x] Force-directed graph layout
- [x] Interactive exploration
- [ ] HTML output format
- [ ] Caller analysis (reverse dependencies)
- [ ] Complexity metrics (cyclomatic complexity)
- [ ] Git blame integration
- [ ] Test function inclusion
- [ ] Context lines around code
- [ ] Export visualizations as PNG/SVG
- [ ] Minimap for large graphs
- [ ] Search and filter nodes
- [ ] Path highlighting
- [ ] Dark mode
Documentation
See docs/ directory for detailed documentation:
SPEC_v2_REVIEW_FOCUSED.md- Complete technical specificationQUICK_START.md- Quick start guide with examplesIMPLEMENTATION_ROADMAP.md- Development roadmapPHASE_2_VISUALIZER.md- Future visualizer plans
License
MIT
Contributing
This project was built using Test-Driven Development. When contributing:
Status
β Phase 1 Complete - Core extraction functionality working with CLI β Phase 2 Complete - Interactive web visualizer
Current capabilities:
- β Symbol location at file:line:column
- β Depth-limited BFS dependency collection
- β Markdown formatting
- β JSON formatting
- β CLI tool
- β Interactive web visualizer with D3.js
- β Force-directed graph layout
- β Zoom, pan, and drag controls
- β Code and documentation viewer
- β External reference tracking
- β Documentation preservation
- β High test coverage (75-78%)
- β Web server for visualizer