A static analysis tool that ensures all goroutines have proper panic recovery via a configurable panic handler (defaults to HandlePanic).
Example use case: ensure that every goroutine defers to your Sentry reporting wrapper, because Sentry needs panics recovered inside each goroutine.
The linter analyzes Go code to find goroutines and ensures that:
- Anonymous goroutines have
defer <target>()as their first statement - Function call goroutines call functions that have
defer <target>()as their first statement - Method call goroutines call methods that have
defer <target>()as their first statement
go install github.com/status-im/goroutine-defer-guard/cmd/goroutine-defer-guard@latest# Run on current directory (defaults to target HandlePanic in the same package)
goroutine-defer-guard ./...
# Skip certain directories
goroutine-defer-guard -skip=./vendor ./...
# Specify a fully-qualified panic handler (import path + function)
goroutine-defer-guard -target=github.com/your/module/common.HandlePanic ./...
# Example: Sentry reporting handler
# Point the linter at your wrapper that reports panics to Sentry:
goroutine-defer-guard -target=github.com/yourorg/observability/panicutil.ReportToSentry ./...go func() {
defer common.HandlePanic()
// ... rest of function
}()go func() {
// Missing defer common.HandlePanic()
// ... rest of function
}()func worker() {
defer common.HandlePanic()
// ... rest of function
}
// Usage
go worker()The linter uses:
- AST analysis to find
gostatements (goroutines) - Go type information to resolve function/method definitions
- Static analysis to verify the first statement is
defer common.HandlePanic()
-target(defaultHandlePanic): fully-qualified panic handler in the formimport/path.Func. If you omit the import path the linter accepts a function in the current package or a selector it can resolve to that name.
- Go 1.21+
Mozilla Public License 2.0