# Go defensive coding

> Go defensive coding is an open-source agent skill for developer tools. Guides Go code away from nil-interface, slice-aliasing and overflow crashes. It is available on Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf and Goose.

- **Kind:** Skill
- **Pricing:** Open source
- **Platforms:** Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf, Goose
- **Website:** https://github.com/eduardo-sl/go-agent-skills/tree/main/skills/(safety)/go-defensive-coding
- **Repository:** https://github.com/eduardo-sl/go-agent-skills
- **GitHub stars:** 90
- **License:** MIT
- **Skill name:** go-defensive-coding
- **Collection:** https://github.com/eduardo-sl/go-agent-skills
- **Categories:** [developer tools](https://ai-powered.com/categories/developer-tools)
- **Listing updated:** 2026-12-02

## About

This skill gives the agent a checklist of Go pitfalls that compile and pass review but fail at runtime: typed-nil interfaces, nil map and slice behaviour, slice aliasing, unchecked numeric narrowing, float and time comparison, defer inside loops, and copying at package boundaries. It works in two modes, hardening code while writing it or reviewing existing code and reporting findings by severity with file and line references.

The skill points to three reference files that the agent reads only when the main rules are not enough, covering nil and aliasing scenarios, numeric safety, and copy and lifecycle rules such as copylocks and noCopy markers. It also recommends running go vet, golangci-lint, staticcheck, gosec and go test with the race detector, and lists a final verification checklist to run through.

It expects a Go project with the Go toolchain installed. golangci-lint and gosec are optional and used only to enforce the rules. Bash access is limited to go, gofmt, golangci-lint and gosec commands.

## When your agent uses it

> Prevent panics, silent corruption, and subtle runtime bugs in Go: typed-nil interfaces, slice aliasing, integer overflow on conversion, float comparison, defer in loops, defensive copying at API boundaries, and zero-value design. Use when hardening code against crashes, reviewing for nil-safety, converting between numeric types, or deciding what to copy at a package boundary. Trigger examples: "nil pointer panic", "why is my error not nil", "slice aliasing", "integer overflow", "compare floats", "defer in a loop", "defensive copy", "make this crash-proof". Not for: data races and goroutine lifecycle (go-concurrency-review), injection and auth (go-security-audit), a panic that already happened (go-troubleshooting).

## What it can access

From a static check of the files on GitHub; nothing was run.

- [Instructions only, no scripts](https://github.com/eduardo-sl/go-agent-skills/tree/HEAD/skills/(safety)/go-defensive-coding)
- [Asks for: Read, Edit, Write, Glob, Grep, Bash(go:*), Bash(gofmt:*), Bash(golangci-lint:*)…](https://github.com/eduardo-sl/go-agent-skills/blob/HEAD/skills/(safety)/go-defensive-coding/SKILL.md)
- [License: MIT](https://github.com/eduardo-sl/go-agent-skills/tree/HEAD/skills/(safety)/go-defensive-coding)

## Features

- **Typed-nil interface checks:** Explains how a non-nil interface can wrap a nil pointer and how to return a true nil on success.
- **Slice aliasing guidance:** Shows how append can overwrite shared backing arrays and how full slice expressions cap the view.
- **Defensive copying rules:** Covers cloning slices and maps passed in or returned from a struct at API boundaries.
- **Numeric conversion safety:** Describes range checks before narrowing integers and avoiding float equality comparisons.
- **Resource and defer review:** Flags defer used inside loops and unchecked errors from deferred Close calls.
- **Lint and vet enforcement:** Lists go vet, golangci-lint, staticcheck, gosec and race-detector commands to verify changes.

## Good for

- Check this Go service for nil pointer panics before release
- Fix a bug where an error value is never nil on success
- Stop append from overwriting data shared between slices
- Add range checks before converting int64 values to int32
- Review a Go package for defensive copies at its public API
- Move defer Close calls out of a loop that opens many files

## Install

### Any agent

Download the skill folder from https://github.com/eduardo-sl/go-agent-skills and copy it into your agent's skills folder.

## FAQ

### What does Go defensive coding need to run?

It requires the Go toolchain. golangci-lint and gosec are optional and used only for enforcing the rules.

### Which problems does Go defensive coding cover?

Typed-nil interfaces, nil map and slice behaviour, slice aliasing, integer overflow on conversion, float and time comparison, defer in loops, defensive copying, and zero-value design.

### What is out of scope?

Data races and goroutine lifecycle, injection and auth issues, and a panic that has already happened. The skill names other skills for those cases.

### Does Go defensive coding change files on its own?

It can edit and write files when hardening code, and it reports findings with severity and file:line references when reviewing.

### Does Go defensive coding run scripts?

No. It is instructions only, with no scripts. This comes from a static check of the files on GitHub; nothing was run. Read the files before you install it.

### Which AI agents can use Go defensive coding?

Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf and Goose. Each reads skills in the SKILL.md format from its own folder; the install steps on this page list them.

### What are the best alternatives to Go defensive coding?

The closest alternatives to Go defensive coding are Go data structures, Go coding standards, Go code review, Go interface design and Go error handling.

## Where this comes from

- Description: from the skill's own SKILL.md.

## Alternatives

- [Go data structures](https://ai-powered.com/skills/go-data-structures): Go data structures is an open-source agent skill for developer tools.
- [Go coding standards](https://ai-powered.com/skills/go-coding-standards): Go coding standards is an open-source agent skill for developer tools.
- [Go code review](https://ai-powered.com/skills/go-code-review): Go code review is an open-source agent skill for developer tools.
- [Go interface design](https://ai-powered.com/skills/go-interface-design): Go interface design is an open-source agent skill for developer tools.
- [Go error handling](https://ai-powered.com/skills/go-error-handling): Go error handling is an open-source agent skill for developer tools.
- [Go security audit](https://ai-powered.com/skills/go-security-audit): Go security audit is an open-source agent skill for security.
- [Go concurrency review](https://ai-powered.com/skills/go-concurrency-review): Go concurrency review is an open-source agent skill for developer tools.
- [Go troubleshooting](https://ai-powered.com/skills/go-troubleshooting): Go troubleshooting is an open-source agent skill for developer tools.

---
Source: https://ai-powered.com/skills/go-defensive-coding
