CLI tool that analyzes Go projects for cost-efficiency anti-patterns using AST-based static analysis. Detects 20 patterns across memory, concurrency, I/O, and error handling categories, then produces a scored report with actionable recommendations.
go install github.com/alpardfm/go-cost-analyzer@latest# Analyze a Go project
go-cost-analyzer ./my-project
# JSON output for CI/CD
go-cost-analyzer -f json -o report.json ./my-project
# Filter specific patterns
go-cost-analyzer -p CEG-016,CEG-017 ./my-project
# Set minimum score threshold (exit 1 if below)
go-cost-analyzer -t 80 ./my-project
# Include test files in analysis
go-cost-analyzer --include-tests ./my-project
# Verbose mode (show per-file progress)
go-cost-analyzer -v ./my-project
# Scale impact projections
go-cost-analyzer --scale 100M ./my-project| Flag | Short | Default | Description |
|---|---|---|---|
| (positional) | required | Path to Go project | |
--output |
-o |
stdout | Write report to file |
--format |
-f |
text |
Output format: text or json |
--verbose |
-v |
false | Show per-file progress to stderr |
--patterns |
-p |
all | Comma-separated pattern filter (IDs or names) |
--threshold |
-t |
0 | Minimum score (exit 1 if below) |
--exclude |
Additional directories to exclude | ||
--include-tests |
false | Include *_test.go files |
|
--scale |
1M |
Impact projection scale: 1M, 10M, 100M |
| Code | Meaning |
|---|---|
| 0 | Success — score above threshold |
| 1 | Score below threshold, or invalid input |
| 2 | Internal error |
| ID | Pattern | Category | Severity |
|---|---|---|---|
| CEG-001 | Batch Processing | IO | Major |
| CEG-002 | Caching Strategies | Memory | Major |
| CEG-003 | Channel Patterns | Concurrency | Minor |
| CEG-004 | Connection Pooling | IO | Critical |
| CEG-005 | Context Cancellation | Concurrency | Critical |
| CEG-006 | Efficient Logging | Memory | Minor |
| CEG-007 | Error Handling | ErrorHandling | Major |
| CEG-008 | Goroutine Leak | Concurrency | Critical |
| CEG-009 | HTTP Client Optimization | IO | Major |
| CEG-010 | Interface Dispatch | Memory | Minor |
| CEG-011 | JSON Processing | Memory | Major |
| CEG-012 | Map Internals | Memory | Minor |
| CEG-013 | Profiling & Benchmarking | Memory | Minor |
| CEG-014 | Query Optimization | IO | Critical |
| CEG-015 | Redis Pipeline | IO | Major |
| CEG-016 | Slice Pre-allocation | Memory | Major |
| CEG-017 | String Building | Memory | Major |
| CEG-018 | Struct Alignment | Memory | Minor |
| CEG-019 | Sync Pool | Memory | Major |
| CEG-020 | Worker Pool | Concurrency | Major |
Create a .cost-analyzer.json in your project root to configure the analyzer without passing flags every time:
{
"exclude": ["internal/generated", "migrations", "pkg/proto"],
"include_tests": false,
"disable_patterns": ["CEG-010", "CEG-013"],
"enable_patterns": [],
"threshold": 70,
"format": "text",
"scale": "10M",
"suppress": [
{
"pattern": "CEG-006",
"paths": ["internal/legacy/**"]
},
{
"pattern": "CEG-018",
"paths": ["pkg/models/*"]
}
]
}| Field | Type | Description |
|---|---|---|
exclude |
string[] | Additional directories to skip from scanning |
include_tests |
bool | Include *_test.go files in analysis |
disable_patterns |
string[] | Pattern IDs to skip entirely |
enable_patterns |
string[] | Only run these patterns (overrides disable) |
threshold |
int | Minimum score — exit 1 if below |
format |
string | Default output format ("text" or "json") |
scale |
string | Impact projection scale ("1M", "10M", "100M") |
suppress |
object[] | Suppress specific patterns in specific paths |
Precedence: CLI flags always override config file values. The config file is optional — if absent, the tool runs with defaults.
Path glob patterns in suppress:
dir/**— match all files recursively underdir/dir/*— match files directly indir/(not subdirectories)dir— match anything underdir/
Each pattern gets a score (0-100) based on the ratio of optimal vs suboptimal occurrences found. The overall score is a weighted average:
- Critical patterns: weight 3
- Major patterns: weight 2
- Minor patterns: weight 1
Patterns with no relevant occurrences are marked "N/A" and excluded from the overall score.
Suppress specific findings with inline comments:
// Suppress a specific pattern on the next line
//noinspect:CEG-016
var items []int
// Suppress on the same line
items = append(items, x) //noinspect:CEG-016
// Suppress entire file (on package declaration line)
package main //noinspect:allSuppressed occurrences are excluded from scoring and shown as a count in the report.
Common cases to consider suppressing:
| Pattern | When to suppress |
|---|---|
| CEG-004 | Connection creation in constructor/factory functions (NewServer, SetupDB) that run once at startup |
| CEG-006 | Logging in error paths, startup code, or CLI tools where throughput isn't a concern |
| CEG-003 | Signal channels (chan struct{}, chan error) used for done/notify patterns |
| CEG-018 | Structs instantiated once (config, singleton) or generated by protobuf/ORM |
Each pattern's reference documentation includes a "When This Is Acceptable" section with detailed guidance.
A template config file is available at .cost-analyzer.example.json — copy it to your project root as .cost-analyzer.json and customize.
# GitHub Actions example
- name: Run cost analysis
run: |
go install github.com/alpardfm/go-cost-analyzer@latest
go-cost-analyzer -f json -t 70 -o cost-report.json .
- name: Upload report
uses: actions/upload-artifact@v4
with:
name: cost-report
path: cost-report.json- Scan — Recursively discovers
.gofiles, skipping vendor/, .git/, testdata/, generated files, and test files - Parse — Parses each file into AST using
go/astandgo/parser - Detect — Runs all 20 pattern detectors (from cost-efficient-go library) against each AST node
- Filter — Applies suppression comments to exclude intentional patterns
- Score — Calculates per-pattern and overall weighted scores
- Report — Outputs colored text or structured JSON with findings, scores, and impact estimates
Processing is concurrent using a bounded worker pool (NumCPU workers) for fast analysis of large projects.
Files with the standard Go generated header are automatically skipped:
// Code generated by protoc-gen-go. DO NOT EDIT.Each finding includes a heuristic impact estimate based on benchmark data from the cost-efficient-go pattern library. Impact levels:
- High — estimated savings > 10 MB at given scale
- Medium — 1-10 MB
- Low — < 1 MB
Use --scale to project impact at different traffic levels (1M, 10M, 100M requests).
Disclaimer: Estimates are based on static analysis and benchmark heuristics. Actual savings depend on runtime behavior and deployment configuration.
MIT