go-defensive

Enforce compile-time interface compliance and safe data handling in Go code.

139|17|Updated Jan 27, 2026
One-click install
npx skills add https://github.com/cxuu/golang-skills --skill go-defensive
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-defensive
Source: https://github.com/cxuu/golang-skills/tree/main/skills/go-defensive
Command: npx skills add https://github.com/cxuu/golang-skills --skill go-defensive

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Go defensive programming patterns help engineers write robust, maintainable Go by enforcing safety boundaries and reducing common runtime errors.

Core Features & Use Cases

  • Interface verification: compile-time checks to ensure types satisfy interfaces.
  • Safe data ownership: copy slices and maps when crossing API boundaries to avoid unintended mutations.
  • Deferral discipline: use defer to ensure resources are released correctly and safely.
  • Avoid mutable globals: inject dependencies to keep code testable and predictable.
  • Explicit field tagging: use struct field tags for stable serialization contracts.
  • Time handling: prefer time.Time and time.Duration to avoid ambiguous raw integers.

Quick Start

Refactor code to enforce interface checks, copy-on-boundary data, and proper defer cleanup for all critical packages.

Frequently Asked Questions about go-defensive

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

FAQPage Schema
How do I enforce compile-time interface compliance in Go?

Safe data handling in Go requires copying slices and maps when crossing API boundaries. This prevents unintended mutations of shared mutable state, ensuring predictable behavior and robust application performance.

What is the best way to manage resource cleanup using defer in Go?

Deferral discipline in Go uses the defer statement to ensure resources are released correctly and safely. This approach guarantees proper cleanup during I/O and concurrency tasks, reducing common runtime errors.

How do I avoid mutable globals to keep my Go code testable?

To avoid mutable globals in Go, inject dependencies directly into your functions and structs. This keeps code testable and predictable by eliminating hidden shared state and enforcing safe data ownership boundaries.

When do I need defensive Go patterns for safe data handling?

Defensive Go patterns are needed when sharing mutable state, performing I/O, or handling concurrency tasks. They enforce safe data copying, proper time handling, and explicit field tagging for stable serialization contracts.

Does defensive Go programming require explicit struct field tags?

Yes, defensive Go programming uses explicit struct field tags to maintain stable serialization contracts. This ensures predictable behavior and robust APIs by preventing ambiguous data handling during marshaling and unmarshaling processes.