golang-safety

Prevents panics, silent data corruption, and runtime bugs in Go code through defensive coding patterns.

1|Updated Feb 2, 2026
One-click install
npx skills add https://github.com/rockcookies/skills --skill golang-safety-rockcookies
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-safety
Source: https://github.com/rockcookies/skills/tree/main/skills/samber-golang/golang-safety
Command: npx skills add https://github.com/rockcookies/skills --skill golang-safety-rockcookies

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go code often contains latent bugs that only surface at runtime: nil pointer panics, silent integer truncation, slice aliasing corruption, and float comparison errors. This Skill provides defensive coding guidelines that catch these mistakes before they reach production. ## Core Features & Use Cases - Nil Safety: Avoids the typed-nil interface trap, nil map write panics, and nil function calls with explicit initialization and guard patterns. - Slice & Map Safety: Prevents append aliasing corruption, subslice memory retention, and unsafe concurrent map access using full-slice expressions and defensive copies. - Numeric & Resource Safety: Enforces bounds checks before integer narrowing, epsilon-based float comparison, and per-iteration resource cleanup instead of defer-in-loop accumulation. - Use Case: When reviewing a Go function that returns an error built from a typed nil pointer, the Skill flags the nil interface trap and rewrites it to return untyped nil on success. ## Quick Start Review this Go file for nil-safety issues, slice aliasing bugs, and unsafe numeric conversions, then fix each problem with defensive coding patterns.

Frequently Asked Questions about golang-safety

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent nil pointer panics in Go?▼

Check pointers for nil before dereferencing, initialize maps with make() before writing, and guard nil function values before calling them. For methods on pointer receivers, add an explicit `if r == nil` guard when nil is a valid state.

Why does my Go function return a non-nil error when nothing failed?▼

This is the typed-nil interface trap: returning a typed nil pointer like `*MyError` through an error interface produces a non-nil interface. Return the untyped `nil` literal explicitly on the success path instead.

How do I safely append to a slice without corrupting the caller's data?▼

Use the full-slice expression `s[:len(s):len(s)]` or `slices.Clone(s)` before appending. This forces a new backing array allocation so the caller's slice is never mutated through shared memory.

Does Go float comparison with == work reliably?▼

No, IEEE 754 arithmetic is not exact, so `0.1 + 0.2 == 0.3` evaluates to false. Use epsilon comparison with `math.Abs(a-b) < epsilon`, choosing 1e-9 for general precision or 0.01 for cent-level financial values.

Why does defer inside a Go loop cause resource leaks?▼

Deferred calls run at function exit, not at loop iteration end, so files or connections accumulate until the function returns. Extract the loop body into a helper function so each defer fires per iteration.

Which linters catch Go safety bugs automatically?▼

Linters including errcheck, forcetypeassert, nilerr, govet, and staticcheck catch many safety pitfalls such as unchecked type assertions and ignored errors. Configure them through golangci-lint in your project.