golang-safety

Enforce defensive Go patterns for nil, slice, map, numeric, and resource safety.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

It prevents common Go mistakes that lead to panics, silent data corruption, and subtle runtime bugs by enforcing defensive patterns around nil handling, slice/map safety, numeric conversion, and resource lifecycles.

Core Features & Use Cases

  • Nil-safety for interfaces, pointers, maps, and funcs: Avoids the typed-nil-in-interface trap, nil function panics, and nil map/slice writes.
  • Slice/map correctness under real constraints: Prevents append aliasing, concurrent map access hazards, subslice backing-array retention, and non-deterministic map iteration when order matters.
  • Numeric and resource lifecycle guardrails: Adds bounds checks for narrowing conversions, uses epsilon comparisons for float equality, and ensures defer is not incorrectly used inside loops.

Use cases:

  • Code review for a Go service that crashes due to nil pointer dereferences or typed-nil error returns.
  • Refactoring exported APIs to avoid leaking internal slices/maps that callers can mutate.
  • Fixing production issues caused by append aliasing, GC retention from subslices, or incorrect float comparisons.

Quick Start

Ask an AI coding agent to review your Go code and refactor any nil-safety, slice/map aliasing, numeric conversion, and loop-defer patterns using the golang-safety skill guidance.

Frequently Asked Questions about golang-safety

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

FAQPage Schema
How do I prevent slice aliasing and backing-array memory leaks when slicing Go arrays?

To prevent slice aliasing and backing-array retention in Go, apply defensive copying for exported getters and avoid subslices that retain the original array. This stops silent data corruption and unintended GC retention when callers mutate returned slices.

What is the best way to compare float values in Go without equality drift?

The best way to compare float values in Go without equality drift is using epsilon comparisons. Direct equality checks fail due to precision loss, so comparing the absolute difference against a small epsilon value prevents silent correctness failures.

How do I safely handle narrowing numeric conversions and overflow checks in Go?

To safely handle narrowing numeric conversions in Go, add explicit bounds checks before casting. This prevents silent truncation and overflow by verifying the source value fits within the target type's range before the unsafe conversion occurs.

Why does using defer inside a Go loop cause resource lifecycle issues?

Using defer inside a Go loop delays resource cleanup until the function exits, not the loop iteration. This causes resource leaks; ensure per-iteration cleanup by calling close functions directly or wrapping loop bodies in separate functions.

How do I fix concurrent map access panics and non-deterministic iteration in Go?

To fix concurrent map access panics in Go, use synchronization primitives like sync.Mutex or channels for safe concurrent writes. For non-deterministic map iteration when order matters, sort keys explicitly before iterating over the map.