golang-safety

Reviews Go code for nil panics, slice aliasing, numeric truncation, and resource lifecycle bugs.

Updated May 9, 2026
One-click install
npx skills add https://github.com/LuminaVault/LuminaVaultShared --skill golang-safety-luminavault
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-safety
Source: https://github.com/LuminaVault/LuminaVaultShared/tree/main/.agents/skills/golang-safety
Command: npx skills add https://github.com/LuminaVault/LuminaVaultShared --skill golang-safety-luminavault

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go code often compiles cleanly yet hides runtime panics and silent data corruption: typed nil pointers wrapped in error interfaces, append calls that mutate shared backing arrays, integer conversions that truncate silently, and defer statements that accumulate resources inside loops. This Skill gives an AI coding agent a defensive-Go checklist so these bugs are caught at authoring or review time instead of in production. ## Core Features & Use Cases - Nil Safety: Detects the typed-nil interface trap, nil map writes, nil func calls, and nil pointer receiver dereferences, with fixes like comma-ok assertions and lazy initialization. - Slice & Map Safety: Prevents append aliasing, subslice backing-array retention, and non-deterministic map iteration using full-slice expressions, slices.Clone, and maps.Clone. - Numeric & Resource Safety: Enforces bounds checks before narrowing conversions, epsilon-based float comparison, and per-iteration resource cleanup by extracting loop bodies into helper functions. - Use Case: While reviewing a pull request that adds a Cache struct with an All() method, the agent flags that returning the internal map directly lets callers bypass Set() and corrupt cache invariants, and rewrites it to return maps.Clone(c.data). ## Quick Start Review this Go file for nil-safety issues, slice aliasing, and defer-in-loop resource leaks, then fix any problems you find.

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, maps, and func values for nil before use, and return untyped nil instead of typed nil pointers from error-returning functions. For zero-value-usable structs, lazy-initialize internal maps inside methods with `if r.m == nil { r.m = make(...) }`.

Why does append in Go sometimes modify the original slice?▼

append reuses the backing array when capacity allows, so both slices share memory and writes through one affect the other. Force a new allocation with the full-slice expression `s[:len(s):len(s)]` or copy first with slices.Clone.

How do I compare float64 values in Go correctly?▼

Use epsilon comparison with math.Abs(a-b) < epsilon instead of ==, because IEEE 754 arithmetic is not exact (0.1+0.2 != 0.3). Use 1e-9 for general precision or 0.01 for cent-level financial comparisons.

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

defer runs at function exit, not at the end of each loop iteration, so files and connections accumulate until the function returns. Extract the loop body into a helper function so each defer fires once per iteration.

What happens when converting int64 to int32 in Go?▼

The conversion truncates silently: values above math.MaxInt32 wrap around (3 billion becomes roughly -1.29 billion) with no error. Always bounds-check against math.MaxInt32 and math.MinInt32 before narrowing and return an error for out-of-range values.

When should Go getters return defensive copies?▼

Return defensive copies whenever an exported method exposes an internal slice or map, since callers can otherwise mutate struct internals through the shared backing array. Use slices.Clone or maps.Clone, which also handle nil inputs correctly.