golang-safety

Analyze Go functions for nil safety risks and unsafe conversions.

2.9k|191|Updated Mar 21, 2026
One-click install
npx skills add https://github.com/samber/cc-skills-golang --skill golang-safety
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-safety
Source: https://github.com/samber/cc-skills-golang/tree/main/skills/golang-safety
Command: npx skills add https://github.com/samber/cc-skills-golang --skill golang-safety

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Golang safety helps teams write robust Go code by preventing nil/interface traps, nil maps, unsafe slice and map aliasing, and silent numeric conversions that lead to panics or subtle bugs.

Core Features & Use Cases

  • Defensive patterns for nil values, zero-value types, and safe initialization in production Go projects.
  • Defensive copying guidance for exported APIs to protect internal state when returning slices or maps.
  • Practical rules & examples for common pitfalls with loops, defer, and conversions, illustrated with concrete code patterns.

Quick Start

Analyze a Go function for nil safety risks and refactor it to use defensive copying and safe initialization patterns.

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 in Go production code?

Prevent nil panics in Go by enforcing explicit nil checks for interfaces, safe initialization for zero-value maps, and defensive patterns before accessing pointers. This approach validates interface nil values and avoids unsafe type conversions that trigger runtime crashes.

Why does my Go map write cause silent data corruption?

Your Go map write causes silent data corruption due to map aliasing when returning internal maps directly. Apply defensive copying for exported APIs to protect internal state, ensuring modifications to returned slices or maps do not silently mutate the original data structure.

What is the best way to avoid slice aliasing bugs in Golang?

The best way to avoid slice aliasing bugs in Golang is to implement defensive copying when returning slices from exported APIs. This protects internal state by creating isolated copies, preventing external modifications from causing silent data corruption across production code.

How do I safely refactor a Go function for nil safety and defensive copying?

Safely refactor a Go function for nil safety by analyzing it for interface nil checks, map write risks, and loop defer usage. Apply safe initialization patterns and defensive copying to exported API returns, ensuring explicit nil checks prevent panics and data corruption.

When do I need defensive copying in Go exported APIs?

You need defensive copying in Go exported APIs whenever returning internal slices or maps that could be modified by callers. This prevents silent data corruption and protects internal state by ensuring external mutations do not alias or overwrite the original data structure.

Can unsafe type conversions cause silent bugs in Go projects?

Unsafe type conversions can cause silent bugs in Go projects by bypassing type safety during numeric or interface conversions. Prevent silent data corruption by applying explicit nil checks and safe initialization patterns to validate types before conversion operations execute.