golang-safety

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

1|Updated May 25, 2020
One-click install
npx skills add https://github.com/titaneric/dotfiles --skill golang-safety-titaneric
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-safety
Source: https://github.com/titaneric/dotfiles/tree/main/dot_agents/skills/golang-safety
Command: npx skills add https://github.com/titaneric/dotfiles --skill golang-safety-titaneric

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go code often contains latent bugs that compile fine but crash or corrupt data at runtime: nil pointer dereferences, typed-nil interface traps, slice aliasing from append, silent integer truncation, and defer accumulation in loops. This Skill guides AI coding agents to write defensive Go code that avoids these pitfalls before they reach production. ## Core Features & Use Cases - Nil Safety: Covers the typed-nil interface trap, nil map write panics, nil function values, and nil pointer receivers, with a deep-dive reference for generics and lazy initialization. - Slice & Map Safety: Addresses append aliasing, subslice backing-array retention, defensive copying with slices.Clone/maps.Clone, and randomized map iteration order. - Numeric & Resource Safety: Enforces bounds checks before integer narrowing, epsilon-based float comparison, division-by-zero guards, and per-iteration resource cleanup instead of defer-in-loop. - Use Case: When reviewing a Go function that returns an error built from a typed nil pointer, or appending to a caller-supplied slice, the Skill flags the trap and provides the corrected pattern with code examples. ## Quick Start Review this Go function for nil-safety issues, slice aliasing bugs, and integer overflow risks, then rewrite it defensively.

Frequently Asked Questions about golang-safety

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

FAQPage Schema
How do I avoid nil pointer panics in Go?

Check for nil before dereferencing pointers, calling function values, or writing to maps. Return untyped nil instead of typed nil pointers from error-returning functions, and use lazy initialization or constructors so zero-value structs are safe to use.

Why does append sometimes modify the original slice in Go?

append reuses the backing array when capacity allows, so both slices share memory and writes to one affect the other. Use the full slice expression s[:len(s):len(s)] or slices.Clone to force a new allocation before appending.

How should I compare float64 values in Go?

Never use == for floats because IEEE 754 arithmetic is inexact (0.1+0.2 != 0.3). Use epsilon comparison with math.Abs(a-b) < epsilon, choosing 1e-9 for general precision or 0.01 for cent-level financial values.

Why is defer inside a for loop a problem in Go?

defer runs at function exit, not loop iteration, so resources like files or HTTP response bodies 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 conversions.

Which linters catch Go safety bugs automatically?

Linters like errcheck, forcetypeassert, nilerr, govet, and staticcheck catch many of these pitfalls, including bare type assertions and unchecked errors. Configure them through golangci-lint in your CI pipeline.