| name | golang-metrics |
| description | Go code metrics: cyclomatic complexity (gocyclo), cognitive complexity (gocognit), lines of code statistics (gocloc), and package-level inventory via go list. Use when asked about code complexity, maintainability scores, code size statistics, or to find the most complex/largest functions. Equivalent to "und metrics" from SciTools Understand.
|
| allowed-tools | Bash(go *), Bash(gocyclo *), Bash(gocognit *), Bash(gocloc *), Read |
Go Code Metrics
This skill covers complexity measurement and code statistics — the Go equivalent of und metrics
from SciTools Understand. Go has no single metrics command; instead, specialized CLI tools
each measure one dimension.
Quick Reference
| Und Command | Go Equivalent | Metric |
|---|
und metrics (complexity) | gocyclo -over N ./... | Cyclomatic complexity |
und metrics (cognitive) | gocognit -top N ./... | Cognitive complexity |
und metrics (LOC) | gocloc ./ | Lines of code |
und metrics (packages) | go list -json ./... | Package inventory |
Tool Installation
which gocyclo || echo "MISSING: go install github.com/fzipp/gocyclo/cmd/gocyclo@latest"
which gocognit || echo "MISSING: go install github.com/uudashr/gocognit/cmd/gocognit@latest"
which gocloc || echo "MISSING: go install github.com/hhatto/gocloc/cmd/gocloc@latest"
Workflow A — Cyclomatic Complexity (gocyclo)
Cyclomatic complexity counts the number of linearly independent paths through code.
Rule of thumb: > 10 = needs attention, > 20 = must refactor.
gocyclo -over 10 ./...
gocyclo -top 15 ./...
gocyclo -over 10 ./cmd/...
gocyclo -over 10 ./internal/...
gocyclo -over 10 -avg ./...
gocyclo -avg ./...
Interpreting output:
15 mypackage FunctionName /path/to/file.go:42:1
^^ complexity ^^ function ^^ location
Workflow B — Cognitive Complexity (gocognit)
Cognitive complexity measures how difficult the code is to read and understand — often a
better proxy for maintainability than cyclomatic complexity.
Rule of thumb: > 15 = hard to read, > 25 = must refactor.
gocognit -top 10 ./...
gocognit -over 15 ./...
gocognit -top 20 ./internal/...
gocognit -over 25 ./... | awk '{print $4": "$2" (complexity "$1")"}' | sort -t: -k1,1
Why both metrics?
gocyclo catches structural branching but misses nesting depth.
gocognit penalizes deep nesting and breaks in linear flow (early returns, goto, recursion).
- For refactoring priorities, use gocognit score > cyclomatic score = deeply nested logic.
Workflow C — Lines of Code Statistics (gocloc)
gocloc ./
gocloc --by-file ./
gocloc --output=json ./ > code-stats.json
gocloc --include-lang=Go --not-match="_test\.go$" ./
gocloc --by-file --output=json ./ | \
jq '.files | to_entries | sort_by(-.value.code) | .[0:20] | .[] | {file: .key, lines: .value.code}'
Output columns:
files — number of files
blank — blank lines
comment — comment lines
code — actual code lines (most important metric)
Workflow D — Package-Level Inventory (go list)
go list ./...
go list ./... | wc -l
go list -json ./... | jq '{
package: .ImportPath,
files: (.GoFiles | length),
deps: (.Imports | length),
testFiles: (.TestGoFiles | length)
}'
go list -json ./... | jq -s 'sort_by(-.Imports | length) | .[0:10] | .[] | {pkg: .ImportPath, deps: (.Imports | length)}'
Workflow E — Combined Metrics Report
Run a full metrics sweep and produce a structured summary:
#!/bin/bash
echo "=============================="
echo " GO CODE METRICS REPORT"
echo " $(date '+%Y-%m-%d %H:%M')"
echo "=============================="
echo ""
echo "--- LINE COUNT (gocloc) ---"
gocloc ./ 2>/dev/null || echo "gocloc not installed"
echo ""
echo "--- TOP 10 CYCLOMATIC COMPLEXITY (gocyclo) ---"
gocyclo -top 10 ./... 2>/dev/null || echo "gocyclo not installed"
echo ""
echo "--- TOP 10 COGNITIVE COMPLEXITY (gocognit) ---"
gocognit -top 10 ./... 2>/dev/null || echo "gocognit not installed"
echo ""
echo "--- FUNCTIONS NEEDING REFACTOR (complexity > 20) ---"
echo "Cyclomatic > 20:"
gocyclo -over 20 ./... 2>/dev/null || echo "(none or gocyclo not installed)"
echo ""
echo "Cognitive > 25:"
gocognit -over 25 ./... 2>/dev/null || echo "(none or gocognit not installed)"
echo ""
echo "--- PACKAGE COUNT ---"
go list ./... | wc -l | xargs -I{} echo "{} packages total"
Thresholds Reference Table
| Metric | Good | Acceptable | Needs Attention | Must Refactor |
|---|
| Cyclomatic (gocyclo) | ≤ 5 | 6–10 | 11–20 | > 20 |
| Cognitive (gocognit) | ≤ 8 | 9–15 | 16–25 | > 25 |
| File LOC | ≤ 200 | 200–500 | 500–1000 | > 1000 |
| Package deps | ≤ 5 | 6–10 | 11–15 | > 15 |
Supporting Files