go-defensive
Skill9 starsChanged 4 months agoMITView on GitHub
Use when hardening Go code at API boundaries: copy slices/maps on entry and return, defer cleanup, verify interface compliance at compile time, model time with time.Time/time.Duration, design enum zero values, prefer crypto/rand, and inject clocks for testability. Apply proactively when reviewing for robustness. Error-handling strategy: see go-error-handling.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- cxuu/golang-skillstheir own version, first seen Jun 20, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- cxuu/golang-skills★ 165same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository26 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- go-clean-architectureSkill
- go-code-reviewSkill
- go-code-styleSkill
- go-concurrencySkill
- go-contextSkill
- go-control-flowSkill
- go-databaseSkill
- go-data-structuresSkill
- go-declarationsSkill
- go-documentationSkill
- go-error-handlingSkill
- go-functional-optionsSkill
- go-functionsSkill
- go-genericsSkill
- go-graphqlSkill
- go-grpcSkill
- go-interfacesSkill
- go-lintingSkill
- go-loggingSkill
- go-namingSkill
- go-observabilitySkill
- go-packagesSkill
- go-performanceSkill
- go-swaggerSkill
- go-testingSkill
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 1 is distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 19
Inject
now func() time.Time: instead of calling time.Now() directly in production code. - Distinctive: line 27
When you touch an exported function or method, walk this list in order:
- Very specific: line 73
Place defer immediately after the acquire — the proximity makes pair-correctness reviewable at a glance.
- Very specific: line 90
If (*Handler) ever stops satisfying http.Handler, the build fails.
- Very specific: line 145
Don't write MustFoo for runtime call sites — it shifts an error condition into a crash.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Go Defensive Programming
Hardening Go code is not paranoia — it is the discipline of making your boundaries honest. Copy what crosses them, clean up what you opened, model time and randomness honestly, and never let a panic escape a package.
Core Rules
- Copy slices and maps at API boundaries. They are reference types — leaking the backing array leaks mutation.
deferthe cleanup right after the acquire.f, err := os.Open(...); defer f.Close().- Verify interface compliance at compile time:
var _ I = (*T)(nil). - Model time and durations with
time.Timeandtime.Duration, never raw ints. - Inject
now func() time.Timeinstead of callingtime.Now()directly in production code. - Enums start at
iota + 1so the zero value is invalid. crypto/randfor secrets, nevermath/rand.- Panics never cross package boundaries. Convert to errors at the edge.
- Avoid mutable package-level state. Inject dependencies instead.
Boundary Hardening Checklist
When you touch an exported function or method, walk this list in order:
| # | Check |
| 1 | Return errors, don't panic across boundaries |
| 2 | Copy slices/maps you'll retain |
| 3 | Copy slices/maps you'll return if internal state aliases them |
| 4 | defer Close / Unlock / cancel right after the acquire |
| 5 | Compile-time interface satisfaction check |
| 6 | time.Time / time.Duration types, injected clock |
| 7 | Enum zero = invalid (iota + 1) |
| 8 | crypto/rand for any secret material |
Copy at API Boundaries
// Receiving: copy a slice we'll retain
func (d *Driver) SetTrips(trips []Trip) {
d.trips = make([]Trip, len(trips))
copy(d.trips, trips)
}
// Returning: copy a map so callers can't mutate our state
func (s *Stats) Snapshot() map[string]int {
out := make(map[string]int, len(s.counters))
for k, v := range s.counters {
out[k] = v
}
return out
}Read references/boundary-copying.md when deciding which boundaries actually need copies (and when copying is wasted work).
Defer Cleanup
defer evaluates arguments at the defer statement and runs the call when the surrounding function returns (LIFO order):
f, err := os.Open(name)
if err != nil {
return err
}
defer f.Close()Place defer immediately after the acquire — the proximity makes pair-correctness reviewable at a glance.
For locks:
mu.Lock()
defer mu.Unlock()Beware of defer inside loops — accumulated defers run only when the function returns, not when the iteration ends.
Verify Interface Compliance
var _ http.Handler = (*Handler)(nil)If (*Handler) ever stops satisfying http.Handler, the build fails. The line costs nothing at runtime and gives you a free contract.
Time Modeling
// Bad — what unit is timeout?
type Config struct {
Timeout int
}
// Good
type Config struct {
Timeout time.Duration
}For wall-clock work, inject the clock so tests can pin time:
type Signer struct {
now func() time.Time
}
func NewSigner() *Signer {
return &Signer{now: time.Now}
}
// In tests:
s := &Signer{now: func() time.Time { return fixedTime }}Read references/time-and-enums.md for monotonic time, time zones, struct tags, and embedding tradeoffs.
Crypto Random
import "crypto/rand"
// Go 1.24+
func APIKey() string { return rand.Text() }math/rand and math/rand/v2 are predictable from a seed — never use them for keys, tokens, nonces, or any secret material.
Must Functions
Must* helpers panic on error. They are appropriate only at program initialization, where failure means the program cannot start:
var (
validID = regexp.MustCompile(`^[a-z][a-z0-9-]{0,62}$`)
tmpl = template.Must(template.ParseFiles("index.html"))
)Don't write MustFoo for runtime call sites — it shifts an error condition into a crash.
Read references/must-and-panic.md for writing custom
Must*, recovering at goroutine boundaries, and distinguishingpanicfromlog.Fatal.
Avoid Mutable Globals
// Bad — testing requires save/restore dance
var DB *sql.DB
// Good — pass the dependency
type Service struct {
db *sql.DB
}Constants and once-initialized lookup tables are fine. Mutable package-level vars are a code smell.
Anti-Patterns
| Anti-pattern | Why it hurts | Do this instead |
| Storing the caller's slice without copying | Mutation aliasing | make + copy |
| Returning the internal map directly | External mutation of state | Return a snapshot |
time.Now() in business logic | Hostile to tests | Inject now func() time.Time |
var Timeout = 5 read as seconds elsewhere | Ambiguous unit | time.Duration |
math/rand for keys | Predictable from seed | crypto/rand |
panic to signal a domain error | Crashes the caller | Return an error |
defer inside a tight loop | Defers stack until function return | Wrap loop body in a function |
Verification Checklist
- Slices/maps stored from callers, or returned aliasing internal state, are copied
- Every
Open/Lockhas adefer Close/Unlocknext to it - Compile-time interface checks cover exported implementations
- Durations are
time.Duration, timestamps aretime.Time; clock is injected - Enum zero values are invalid (or explicitly meaningful)
- No secret material derived from
math/rand - No mutable package-level vars; no
panicacross library boundaries
References
- references/boundary-copying.md — when defensive copies pay off vs. wasted allocation
- references/time-and-enums.md — modeling time, durations, enums, struct tags
- references/must-and-panic.md —
Must*helpers, recover at boundaries, panic vslog.Fatal
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

