golang-safety

Identify nil-safety and defensive-coding gaps in Go codebases.

7|1|Updated Mar 15, 2026
One-click install
npx skills add https://github.com/Harmeet10000/skills --skill golang-safety-harmeet10000
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-safety
Source: https://github.com/Harmeet10000/skills/tree/main/skills/backend/Golang/golang-safety
Command: npx skills add https://github.com/Harmeet10000/skills --skill golang-safety-harmeet10000

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Golang-safety helps developers prevent panics, nil interface traps, and subtle runtime bugs by applying defensive coding, nil-safety patterns, and safe initialization across common Go patterns.

Core Features & Use Cases

  • Nil-safety patterns for interfaces, maps, slices, and pointers to avoid typed nil traps.
  • Defensive copying to protect internal state in exported data structures.
  • Lazy initialization and zero-value safety to make the zero value safe to use.
  • Practical guidance for common pitfalls like defer-in-loops, safe type assertions, and concurrency basics.
  • Tooling and code-review patterns to enforce safety in Go projects.

Quick Start

Audit a sample Go project and implement the provided nil-safety and defensive-coding patterns step by step.

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 interface panics in Go?

To prevent nil interface panics in Go, apply nil-safety patterns that check interface types before invocation and avoid typed nil traps by ensuring interfaces are fully initialized before returning them from functions.

Why does writing to an uninitialized Go map cause a panic?

Writing to an uninitialized Go map causes a panic because nil maps cannot be assigned to. You must use lazy initialization with the make function to safely allocate the map before inserting data.

What is the best way to handle safe type assertions in Golang?

The best way to handle safe type assertions in Golang is using the comma-ok idiom. This pattern prevents runtime panics by returning a boolean alongside the asserted value to verify the assertion's success.

How do I avoid resource leaks with defer inside Go loops?

To avoid resource leaks with defer inside Go loops, extract the loop body into a separate function or ensure explicit closure calls. Deferring inside loops accumulates resources until the outer function exits.

Does defensive coding in Go require copying slices and maps?

Defensive coding in Go requires copying slices and maps when exporting internal state. Defensive copying protects exported data structures from external mutation, ensuring internal state remains safe and unmodified.

What are the limitations of zero-value safety in Golang?

A limitation of zero-value safety in Golang is that zero values are not universally safe; while slices and pointers are safely nilable, nil maps and interfaces still cause panics if invoked or written to without initialization.