victorzhuk/skills

z-go-troubleshooting

Systematic Go debugging — reproduce, instrument, find root cause, verify fix.

Quelltext ansehen

Verwendung

  1. Installationsbefehl kopieren und das Quellpaket vor der Installation in der eigenen Agentenumgebung prüfen.
  2. Benötigte Werkzeuge, Zugangsdaten und Hosts im Original prüfen.
  3. Eingabe und gewünschtes Ergebnis beschreiben und die Ausgabe mit den Quellen abgleichen.
npx skills add https://github.com/victorzhuk/skills --skill z-go-troubleshooting

Vor dem Start

  • Die Installation umfasst weder Modellabo noch API-Kontingent oder Dateizugriffsrechte.
  • Sterne und Installationen zeigen Aktivität, keine unabhängige Qualitäts- oder Sicherheitsprüfung.
Originales Skill-Dokument

Aus dem Quell-Repository gerendert; Überschriften, Beispiele, Code, Tabellen, Links und Bilder bleiben erhalten.

Go troubleshooting

No fixes without a root cause. Symptom patches create new bugs. This is especially true under time pressure.

Feedback loop first

Before forming any hypothesis, build the tightest pass/fail signal you can — fast (reruns in seconds), deterministic (same result every run), sharp (fails close to the fault). Jumping straight to a hypothesis is the exact failure this discipline prevents.

Prefer the tightest loop you can build:

  1. A failing unit test reproducing the bug in-package.
  2. A failing integration test against the real boundary (DB/HTTP/queue via testcontainers).
  3. A scripted CLI / curl repro that fails deterministically.
  4. git bisect against a known-good commit (regressions).
  5. A one-off instrumented build — extra logs/metrics, GODEBUG, -gcflags — run against a live repro.
  6. Logs/traces captured at the moment of the symptom (loosest; no local repro yet).

Tighten before you widen: make the loop faster (-count, smaller input), more deterministic (-race, fixed seed, synctest), and sharper (assert nearer the suspected fault) before changing code.

Decision tree

SymptomFirst move
Build failsgo build ./... 2>&1, then go vet ./...
Wrong output / logicWrite a failing test; check error handling, nil, off-by-one
Panic / crashGOTRACEBACK=all ./app, read the full stack
Flaky / racetimeout 5m go test -parallel 4 -p 2 -race -timeout 5m ./...
Hang / frozencurl localhost:6060/debug/pprof/goroutine?debug=2
High CPUpprof CPU profile → flamegraph
Memory growingpprof heap profile (allocs vs inuse_space)
High p99 latencymutex + block + goroutine profiles together

Golden rules

  1. Read the full error — file, line, type. Go there first.
  2. Reproduce before fixing. Write a failing test; use git bisect for regressions.
  3. One hypothesis at a time. Change one thing, measure, confirm.
  4. Measure, don't guess — race detector, pprof, benchmarks.
  5. Trace to root cause. Band-aids on symptoms create new bugs.
  6. Check callers first — the "bug" may be unreachable in prod.
  7. fmt.Println locally is fine; escalate tools only when needed.

Common Go bug patterns

Nil pointer / typed-nil interface trap

go
var p *MyType = nil
var i MyInterface = p  // i != nil — interface has non-nil type

Always compare to the concrete type, or return nil explicitly from functions returning an interface. Recognize it here; the defense lives in [[z-go-safety]].

Variable shadowing

go
x, err := foo()
if err != nil {
    x, err := bar()  // new x, err — outer x unchanged
    _ = x
}

Use = for the outer variable; only := the new one.

Goroutine leak — always defer cancel() right after context.WithCancel. Detect with goleak.VerifyNone(t) in tests.

Slice aliasing — b := a[:2] shares memory; append may overwrite a[2]. Copy when you don't own the slice: b = append([]int(nil), a[:2]...). Same append-aliasing trap [[z-go-safety]] covers as a design-time rule.

Concurrent map write — panics at runtime; use sync.Map or sync.RWMutex. go test -race catches this. [[z-go-safety]] has the nil-map and concurrent-write prevention patterns.

Error swallowing — never use _ for errors; propagate with fmt.Errorf("do thing: %w", err). [[z-go-errors]] covers the wrapping and handling rules this violates.

defer in a loop — defers accumulate until the function returns, not the iteration. Wrap the body in a closure or extract a helper. [[z-go-safety]] has the general defer-discipline rule.

Race conditions and deadlocks

sh
timeout 5m go test -parallel 4 -p 2 -race -timeout 5m ./...
GORACE="log_path=/tmp/race" timeout 5m go test -parallel 4 -p 2 -race -timeout 5m ./...

For a running service:

sh
kill -SIGABRT <pid>   # dumps all goroutine stacks
# or
curl localhost:6060/debug/pprof/goroutine?debug=2

Deadlock signatures: all goroutines are asleep, cyclic lock acquisition, channel send/recv with no other side, WaitGroup counter goes negative.

pprof quick reference

Enable endpoint (dev/staging only — add auth in prod):

go
import _ "net/http/pprof"
go http.ListenAndServe(":6060", nil)

Capture profiles:

sh
# CPU (30s)
go tool pprof -http=:8080 http://localhost:6060/debug/pprof/profile?seconds=30

# Heap
go tool pprof -http=:8080 http://localhost:6060/debug/pprof/heap

# Goroutines
go tool pprof -http=:8080 http://localhost:6060/debug/pprof/goroutine

# Mutex contention (requires runtime.SetMutexProfileFraction(1))
go tool pprof -http=:8080 http://localhost:6060/debug/pprof/mutex

Compare heap snapshots to isolate a leak:

sh
go tool pprof -base base.pb.gz current.pb.gz

Delve (breakpoint debugging)

sh
dlv test ./internal/mypackage -- -test.run TestMyCase
dlv attach <pid>

Useful commands: b main.go:42, c, n, s, p myVar, goroutines, goroutine <id> bt.

GODEBUG tracing

sh
GODEBUG=gctrace=1 ./app          # GC pause frequency and size
GODEBUG=schedtrace=1000 ./app    # scheduler events every 1s
GODEBUG=asyncpreemptoff=1 ./app  # disable async preemption (debugging only)

Escape analysis (find unintended heap allocations):

sh
go build -gcflags="-m=2" ./... 2>&1 | grep "escapes to heap"

Test-driven debugging

Default to a regression test before the fix at the agreed seam. Preserve an approved tests-after flow. Either way, prove the test fails against the unfixed code and passes after the fix. Flaky test? -count=100 -race amplifies; goleak.VerifyNone(t) for leaks.

Production & hygiene

  • Use slog in prod — never fmt.Println. Check logs before attaching a profiler.
  • pprof behind auth only (mTLS / internal network). Capture at the moment of the symptom.
  • Set GOTRACEBACK=all in the systemd unit or pod spec, not in code.
  • Never recover panics you don't understand; never disable -race to pass tests.
  • Never scatter nil guards — find why nil appears.

Verify

sh
timeout 5m go test -parallel 4 -p 2 -race -timeout 5m ./...
go vet ./...
golangci-lint run
aus demselben Repository

Weitere Skills

Alle Skills
victorzhuk
Community

z-accessibility

Audit and improve web accessibility following WCAG 2.2 guidelines. Use when asked to "improve accessibility", "a11y audit", "WCAG compliance", "screen reader support", "keyboard navigation", or "make accessible".

Installationen
1
GitHub Stars
0
Aktualisiert
22. Okt.
victorzhuk
Community

z-algo-core

DS/algorithm selection judgment for production code, not competitive tricks — hash map for membership/dedup, slice+sort beats a tree until proven, heap for top-k, two-pointer/sliding-window for scans, BFS/DFS/topo-sort for dependency shapes. Triggers on Big-O, hash map vs tree. Does not cover profiling/tuning; see [[z-go-performance]].

Installationen
1
GitHub Stars
0
Aktualisiert
22. Okt.
victorzhuk
Community

z-bdd

BDD acceptance contracts for humans and agents across UI, API, and CLI products. Use for Gherkin, Given-When-Then, E2E acceptance, or scenario evidence. Define repeatable scenarios with explicit assertions and required outcomes. Test selection belongs to [[z-testing-strategy]]. Go harness wiring belongs to [[z-go-bdd]].

Installationen
1
GitHub Stars
0
Aktualisiert
22. Okt.
victorzhuk
Community

z-codegraph

Use an existing CodeGraph integration for call graphs, impact analysis, affected tests, or route discovery. Discover installed tools and index freshness; follow host LSP precedence and verify current source. Do not initialize indexes without user authorization.

Installationen
1
GitHub Stars
0
Aktualisiert
22. Okt.