go-defensive

Harden Go API boundaries against mutation leaks, panics, weak randomness, and time ambiguity.

8|Updated Jun 5, 2026
One-click install
npx skills add https://github.com/muratmirgun/gophers --skill go-defensive-muratmirgun
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-defensive
Source: https://github.com/muratmirgun/gophers/tree/main/skills/go-defensive
Command: npx skills add https://github.com/muratmirgun/gophers --skill go-defensive-muratmirgun

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you make Go code safer at package and API boundaries by preventing hidden mutation, unclear time handling, accidental panics, and weak randomness. It reduces bugs that appear when callers can modify your internal state or when code is hard to test and reason about.

Core Features & Use Cases

  • Boundary copying: Copy slices and maps on input or output when ownership should not be shared.
  • Cleanup discipline: Place defer right after acquire for files, locks, and other resources.
  • Robust modeling: Use time.Time, time.Duration, injected clocks, and invalid zero-value enums for clearer APIs.
  • Safety checks: Verify interface compliance at compile time, avoid mutable globals, and use crypto-rand for secrets.
  • Use case: Review an exported Go service method and harden it so callers cannot mutate internal state, tests can control time, and errors are returned safely instead of panicking.

Quick Start

Apply the go-defensive skill to this Go package and review its exported APIs for boundary copying, cleanup placement, time modeling, enum safety, and panic-free error handling.

Frequently Asked Questions about go-defensive

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

FAQPage Schema
How do I prevent callers from mutating internal slices and maps in Go exported functions?

Preventing internal state mutation requires boundary copying in Go, duplicating slices and maps on input or output when ownership should not be shared. This stops callers from modifying your package's internal data.

What is the best way to handle time in Go APIs to keep business logic testable?

Handling time testably in Go APIs requires using time.Time, time.Duration, and injected clocks. This approach allows tests to control time precisely, ensuring your exported business logic remains deterministic and robust.

How do I avoid accidental panics propagating through my Go library interfaces?

Avoiding accidental panic propagation in Go library interfaces requires returning errors safely instead of panicking. You must also enforce cleanup discipline by placing defer statements immediately after resource acquisition like locks or files.

Why should I use crypto/rand instead of math/rand for secret material in Go?

Using crypto/rand for secret material in Go prevents weak randomness vulnerabilities. Unlike math/rand, crypto/rand provides cryptographically secure random numbers, which is a required safety check for hardening exported API boundaries.

How do I enforce compile-time interface compliance for Go API boundaries?

Enforcing compile-time interface compliance in Go requires adding compile-time interface checks to your exported functions and methods. This verifies that types satisfy contracts before runtime, preventing ambiguous zero-value enums and leaks.

Does this defensive programming approach work for existing Go packages with mutable globals?

Yes, this defensive programming approach works for existing Go packages by reviewing exported APIs to avoid mutable globals. It hardens library interfaces by requiring boundary copying, injected clocks, and invalid zero-value enums for safer state.