Skip to content
Back to skills

Go Patterns

ASecurity

Use when creating or modifying Go files under src/backend/ — HTTP handlers, services, stdlib net/http routers, context propagation, and table-driven tests. Triggers on any .go file change under src/backend/. Covers idiomatic Go, error wrapping, context handling, and concurrency patterns.

  • 6 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 4, 2026
developmentgotestingbackend

Security analysis

A100/100

Pro scans all 6 files and shows the line behind each finding

Scanned September 4, 2026

npx -y skills add kouroshez/coding-os --skill go-patterns --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Go Patterns?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Go Patterns
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/kouroshez-go-patterns/badge)](https://www.skillsdirectory.com/skills/kouroshez-go-patterns)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: go-patterns
tier: stack
domain: [backend]
description: Use when creating or modifying Go files under src/backend/ — HTTP handlers, services, stdlib net/http routers, context propagation, and table-driven tests. Triggers on any .go file change under src/backend/. Covers idiomatic Go, error wrapping, context handling, and concurrency patterns.
globs: "src/backend/**/*.go"
depends_on:
  - clean-code
  - backend-fundamentals
last_reviewed: "2026-06-04"
versions_ref: versions.json

---

REQUIRED BACKGROUND: You MUST also follow the clean-code skill (`.claude/skills/clean-code/SKILL.md`). That skill covers universal principles (fail-closed errors, self-documenting code, edge cases, error path tests). This skill adds Go-specific patterns on top.

## Pre-Code Checklist

- [ ] Read `docs/engineering/go-rules.md` — canonical backend policy
- [ ] If touching HTTP handlers: read `docs/playbooks/go-service.md`
- [ ] Confirm Go version (≥ 1.22 recommended for range-over-func)
- [ ] Search the repo with Grep/Glob for existing code before creating a new file

## 1. Project layout

```
src/backend/
├── cmd/<app>/main.go        # entry point, wire dependencies
├── internal/
│   ├── http/                # handlers, middleware
│   ├── service/             # business logic (no net/http types)
│   ├── repo/                # data access
│   └── model/               # domain types
└── pkg/                     # optional: packages intended for external reuse
```

## 2. Error handling

- Return errors, don't panic. Panic is for programming errors, not runtime conditions.
- Wrap with `fmt.Errorf("operation: %w", err)` to preserve the chain.
- Define sentinel errors for the domain: `var ErrNotFound = errors.New("not found")`.
- Check with `errors.Is` / `errors.As`, never with string matching.

## 3. Context

- First argument of every function that crosses a service boundary: `ctx context.Context`.
- Never store context in struct fields.
- Propagate cancellation through service → repo → DB driver.

## 4. Concurrency

- Goroutines need a clear owner and a way to stop. No fire-and-forget unless the lifetime is the process.
- Use `sync.WaitGroup` + `context.Context` cancellation, or `errgroup.Group`.
- Channels ≠ shared memory. Prefer simple mutex + struct when the data is small and fast.

## 5. Testing

- Table-driven tests. Subtests via `t.Run(tc.name, ...)`.
- `testing.T.TempDir()` for filesystem fixtures.
- Race detector in CI: `go test -race ./...`.
- Coverage target: ≥ 80%.

## Deepening + tooling

- [references/go-2026-idioms.md](references/go-2026-idioms.md) — errors, naming, concurrency, generics, tests (per Google's Go Style Guide).
- [references/anatomy.md](references/anatomy.md) — file map + scaffold; `scripts/new_endpoint.py` to generate one.
- [assets/go-checklist.md](assets/go-checklist.md) — the review gate.
- Versions pinned in [versions.json](versions.json) — `make skills-check-versions`.

Files in this skill

  • SKILL.md2.9 KB
  • assets/go-checklist.md1.3 KB
  • references/anatomy.md4.7 KB
  • references/go-2026-idioms.md3.2 KB
  • scripts/new_endpoint.py4.4 KB
  • versions.json366 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.