golang-safety

Apply defensive Go coding practices to prevent runtime panics and data corruption.

1|Updated May 27, 2026
One-click install
npx skills add https://github.com/dmwin72015/netdisk --skill golang-safety-dmwin72015
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-safety
Source: https://github.com/dmwin72015/netdisk/tree/main/.agents/skills/golang-safety
Command: npx skills add https://github.com/dmwin72015/netdisk --skill golang-safety-dmwin72015

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Prevents common Go mistakes that cause runtime panics, concurrency hazards, and subtle silent data corruption—especially around nils, slicing/appending, maps, numeric narrowing, and resource lifecycles.

Core Features & Use Cases

  • Nil-safety patterns: Avoid typed-nil traps in interfaces/errors, guard nil pointer receivers and nil function fields, and design zero-value-safe APIs with lazy initialization.
  • Slice & map safety: Prevent append aliasing/caller-slice corruption, avoid subslice backing-array retention, and ensure maps are not accessed concurrently without synchronization.
  • Defensive correctness checks: Enforce bounds before integer narrowing, avoid float equality pitfalls with epsilon comparisons, and prevent defer-in-loops resource accumulation (HTTP bodies, files, DB connections).

Use case: Reviewing a Go service that sometimes panics under edge inputs (nil configs, empty callbacks) or shows unexplained data corruption after “safe” slice operations—this skill provides targeted guardrails and review checklists to make the code robust.

Quick Start

Ask an AI coding agent to review a specific Go code path (e.g., an HTTP upload handler and its storage helpers) specifically for nil-safety, slice/map aliasing risks, numeric narrowing, float comparison, and defer-in-loop issues, then propose concrete fixes.

Frequently Asked Questions about golang-safety

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

FAQPage Schema
How do I prevent silent data corruption from slice aliasing and append in Go?

Silent data corruption from slice aliasing in Go happens when appending overwrites a caller's backing array. Prevent this by making defensive copies for exported getters and applying safe slice handling practices during code review.

How do I stop concurrent map access panics in my Go service?

Concurrent map access in Go causes fatal runtime panics. Prevent this by implementing concurrency-safe map behavior using proper synchronization mechanisms, ensuring maps are never accessed simultaneously without explicit locks.

What is the best way to handle nil maps and zero-value-safe APIs in Go?

The best way to handle nil maps in Go is designing zero-value-safe APIs with lazy initialization. This approach ensures your code avoids nil-map initialization panics and remains robust even with uninitialized configurations.

How do I prevent resource leaks from defer statements inside Go loops?

Prevent defer-in-loops resource accumulation in Go by executing per-iteration defer cleanup explicitly. This practice stops HTTP bodies, files, and DB connections from accumulating memory until the function returns.

How do I avoid numeric narrowing and float comparison errors in Go?

Avoid numeric narrowing errors in Go by enforcing bounds checks before integer conversions. Prevent float equality pitfalls by using epsilon-based comparisons instead of direct equality checks for floating-point values.