# Improve Go Error Handling

Go error paths often lose context, duplicate logs, or expose internals. This skill gives Claude, Codex, and Claude Code concrete rules for safer Go errors.

## Install

```bash
npx skillstore add samber/golang-error-handling
```

## Metadata

- Status: approved
- Slug: samber-golang-error-handling
- Version: 1.2.0
- Author version: 1.2.0
- Skillstore revision: r1
- Version status: valid
- Tree hash: eaaa86f67b8e5d75a7943595b540452cf5dc5219b6936c99e0d33daef01e64bc
- Author: samber
- GitHub username: samber
- License: MIT
- Repository: https://github.com/samber/cc-skills-golang/tree/main/skills/golang-error-handling/
- Ref: e397ce5a135369cd6def89cb6400d013e2f68f2f
- Supported tools: Claude, Codex, Claude Code
- Audit status: complete
- Agent install advisory: allowed
- Manual install advisory: allowed
- Artifact signature: available
- Audit attestation: unavailable
- Human verification: not\_verified
- Risk factors: external\_commands, network
- Quality score: 78
- Quality tier: bronze
- Public page: https://skillstore.pages.dev/skills/samber-golang-error-handling
- Manifest: https://skillstore.pages.dev/api/skills/samber-golang-error-handling/manifest

## Capabilities

- Explains when to use errors.New, fmt.Errorf, custom error types, sentinel errors, and samber/oops.
- Shows how to preserve chains with %w and break public boundaries with %v.
- Guides matching wrapped errors with errors.Is, errors.As, and Go 1.26 errors.AsType.
- Uses errors.Join for validation, cleanup, and independent failure collection.
- Applies the single handling rule with slog and structured attributes.
- Provides evaluation prompts for common production error-handling traps.

## Use Cases

- Service Developer: Add context-rich returned errors without duplicate logs or high-cardinality messages.
- Code Reviewer: Review pull requests for swallowed errors, unsafe panic use, and incorrect wrapped-error inspection.
- Platform Maintainer: Standardize structured error logs and public boundary behavior across Go services.

## Prompt Templates

### Check Basic Error Handling

```
Review this Go function for unchecked errors, lowercase messages, and useful wrapping context. Suggest minimal fixes.
```

### Refactor Wrapped Error Matching

```
Update this Go code to use errors.Is and errors.As correctly. Preserve behavior for wrapped errors and explain each change.
```

### Modernize Production Logging

```
Modernize this Go error path with slog and structured attributes. Follow the single handling rule and avoid high-cardinality messages.
```

### Audit Error Strategy Across Modules

```
Audit these Go packages for error creation, wrapping, panic recovery, joined errors, and public API boundaries. Return prioritized findings and fixes.
```

## Limitations

- It is guidance for AI coding agents, not a compiler or static analyzer.
- It assumes Go projects can use recent standard library features when available.
- samber/oops and slog helper libraries remain optional external dependencies.
- Large audits still need human review for domain-specific error semantics.

## Best Practices

- Use %w inside module boundaries and %v when intentionally hiding internal error types.
- Log or return each error once, with structured attributes for IDs and counts.
- Keep stable messages lowercase and low-cardinality; place variable data in attributes.

## Anti Patterns

- Discarding returned errors with blank identifiers or using results before checking errors.
- Logging an error at every layer and returning it again.
- Interpolating user, tenant, path, or row IDs into stable production messages.

## Security Audit

- Audited at: 2026-07-06T20:51:38.847\+00:00
- Summary: All static findings appear to be false positives from Markdown code spans, JSON evaluation prompts, and illustrative Go examples. I found no prompt injection, data exfiltration intent, unsafe command execution, or automatic network activity in the reviewed files.

## Stats

- Views: 2
- Downloads: 7
- Favorites: 0
- Popularity score: 0
