golang-safety

Review Go code for nil safety, slice/map ownership, numeric conversions, and resource lifecycles.

2|Updated Feb 12, 2024
One-click install
npx skills add https://github.com/adibfirman/dotfiles --skill golang-safety-adibfirman
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-safety
Source: https://github.com/adibfirman/dotfiles/tree/main/claude/.claude/skills/technical/golang/golang-safety
Command: npx skills add https://github.com/adibfirman/dotfiles --skill golang-safety-adibfirman

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

It helps you write defensive Go code that avoids common crash paths (nil dereferences, nil map writes, nil function calls) and correctness failures that otherwise surface as subtle runtime bugs or silent data corruption.

Core Features & Use Cases

  • Nil safety: prevents typed-nil interface traps, nil pointer receiver panics, nil function panics, and nil map write panics.
  • Slice/map correctness: avoids append backing-array aliasing, subslice backing-array retention, unsafe concurrent map access patterns, and ensures deterministic results when iterating maps.
  • Numeric & resource safety: catches silent truncation from integer narrowing, reduces float comparison pitfalls, and prevents defer misuse inside loops that accumulates resources.

Use case: You are reviewing a Go pull request that sometimes panics in production and occasionally produces incorrect results—apply this to audit nil handling, slice/map ownership, numeric conversions, and lifecycle patterns like defer placement.

Quick Start

Use golang-safety to review the Go file you are working on and fix any nil-safety, slice/map aliasing, numeric conversion, float comparison, and defer-in-loop issues found.

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 panics and silent data corruption in Go?

Prevent nil panics and silent data corruption in Go by applying strict guardrails like safe type assertions, explicit nil returns in interfaces, and defensive nil checks before pointer receiver method calls to stop runtime crashes before they happen.

Why does my Go code panic when writing to a nil map?

Your Go code panics when writing to a nil map because uninitialized maps cannot hold values; you must explicitly initialize maps with make before insertion to avoid nil map write panics and ensure safe concurrent access patterns.

How does slice append aliasing cause silent bugs in Go?

Slice append aliasing causes silent bugs in Go when multiple slices share the same backing array after append, meaning modifications to one slice unexpectedly mutate the other; defensive copies prevent this subslice backing-array retention.

What is the best way to avoid typed-nil interface traps in defensive Go code?

The best way to avoid typed-nil interface traps in defensive Go code is to use safe type assertions and return explicit nil values from interfaces rather than returning typed nil pointers that satisfy non-nil interface contracts.

When do I need to use epsilon-based float comparisons in Go?

You need to use epsilon-based float comparisons in Go when checking floating-point equality to avoid precision pitfalls, ensuring that silent truncation from integer narrowing conversions and numeric correctness failures are caught during code review.

Does defer inside a loop cause resource accumulation in Go?

Yes, defer inside a loop causes resource accumulation in Go because deferred functions only execute when the enclosing function returns; use per-iteration cleanup instead of defer-in-loops to manage resource lifecycles deterministically.