go-defensive

Enforce defensive Go patterns for interfaces, slices, maps, and defer cleanup.

Updated Feb 28, 2026
One-click install
npx skills add https://github.com/phant-app/phant --skill go-defensive-phant-app
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-defensive
Source: https://github.com/phant-app/phant/tree/main/.agents/skills/go-defensive
Command: npx skills add https://github.com/phant-app/phant --skill go-defensive-phant-app

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Go developers frequently face subtle runtime issues stemming from interface misuse, shared mutable state, and cleanup omissions. This Skill shares practical defensive programming patterns to validate interfaces at compile time, copy slices and maps safely at API boundaries, and manage resources using defer for reliable cleanup.

Core Features & Use Cases

  • Interface compliance verification via compile-time nil assertions to ensure type compatibility.
  • Safe data handling by copying slices and maps at API boundaries to prevent external mutations.
  • Determined resource lifecycle management with defer to ensure proper cleanup in concurrent code.
  • Use cases include building robust Go libraries and services that expose interfaces, manage shared data, and require predictable cleanup.

Quick Start

Implement interface compliance checks and safe slice copies in a simple Go module.

Frequently Asked Questions about go-defensive

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

FAQPage Schema
How do I verify Go interface compliance at compile time to prevent runtime panics?

Prevent external mutations of shared mutable state by copying slices and maps at API boundaries. This defensive pattern ensures safe data handling in Go by isolating internal structures from external modifications.

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

The best way to manage resource cleanup in concurrent Go code is using defer for structured lifecycle management. This pattern ensures deterministic cleanup of resources like locks and connections, preventing leaks in concurrent execution.

Can I apply these defensive programming patterns to a Go library that exposes interfaces?

Yes, these defensive patterns specifically target Go library authors and services that expose interfaces or manage shared data. They provide static checks and clear lifecycle management to build robust, predictable Go modules.

How do I safely copy slices and maps in Go to prevent external mutations?

Safely copy slices and maps at API boundaries to prevent external mutations of shared mutable state. This defensive data handling pattern isolates internal structures from unexpected modifications by callers.

Why does shared mutable state cause subtle runtime bugs in Go and how can I prevent it?

Shared mutable state causes subtle runtime bugs in Go when concurrent code modifies data unexpectedly. Prevent this by copying slices and maps at API boundaries and applying defensive programming patterns to isolate state.