go-defensive

Apply defensive Go patterns for interface verification, safe copying, and defer cleanup.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Go projects often suffer from subtle bugs due to interface mismatches, unsafe in-place modifications, improper resource cleanup, and poor time handling. This Skill provides a collection of defensive programming patterns to prevent these issues by enforcing compile-time checks, safe data handling, and reliable cleanup.

Core Features & Use Cases

  • Verify interface compliance at compile time using zero-value assertions to catch mismatches early.
  • Copy slices and maps at API boundaries to avoid exposing internal state.
  • Use defer for cleanup to ensure resources are released reliably, even on early returns.
  • Use time.Time and time.Duration for time values to avoid ambiguous numeric representations.
  • Favor dependency injection over mutable globals to improve testability and robustness.
  • Avoid embedding public structs; delegate explicitly to preserve API evolution.

Quick Start

Refactor a small Go module to apply interface verification, safe copying, and defer-based cleanup for robust production-grade code.

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 errors?

Verify Go interface compliance at compile time by using zero-value assertions to catch interface mismatches early, ensuring struct implementations meet required contracts before deployment and preventing common runtime panics.

What's the best way to copy slices and maps at Go API boundaries to protect internal state?

Copy slices and maps at Go API boundaries to avoid exposing internal state by duplicating data structures before returning them, preventing external callers from accidentally modifying internal collections and causing subtle bugs.

How do I ensure deterministic resource cleanup with defer in Go production code?

Ensure deterministic resource cleanup with defer in Go production code by scheduling resource release immediately after acquisition, guaranteeing files and connections close reliably even on early returns or unexpected errors.

Why does Go defensive programming favor dependency injection over mutable global state?

Go defensive programming favors dependency injection over mutable global state to improve testability and robustness, minimizing side effects by explicitly passing dependencies rather than relying on shared variables that cause unpredictable behavior.

How do I handle time values safely in Go to avoid ambiguous numeric representations?

Handle time values safely in Go by using time.Time and time.Duration for all time-related fields, avoiding ambiguous numeric representations and ensuring proper time handling across typical Go projects with explicit typing.

When should I avoid embedding public structs in Go API design?

Avoid embedding public structs in Go API design when you need to preserve API evolution, delegating explicitly instead to prevent exposing internal fields and methods that could break backward compatibility in future releases.