golang-safety

Apply defensive programming techniques to write panic-free Go code.

Updated May 4, 2026
One-click install
npx skills add https://github.com/VIethoangnguyenle/nexus-enterprise --skill golang-safety-viethoangnguyenle
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-safety
Source: https://github.com/VIethoangnguyenle/nexus-enterprise/tree/main/.agent/skills/golang-safety
Command: npx skills add https://github.com/VIethoangnguyenle/nexus-enterprise --skill golang-safety-viethoangnguyenle

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

It helps developers write more robust and panic-resistant Go code by following defensive programming practices.

Core Features & Use Cases

  • Nil and memory safety: Guides on avoiding nil pointer dereferences and improper use of nil maps, slices, and channels.
  • Numeric and type conversion safeguards: Provides techniques to prevent silent truncation and overflow during type conversions.
  • Resource management: Advises on correctly closing resources like files with defer inside loops.
  • Concurrency safety: Emphasizes safe map usage with concurrency primitives.
  • Use Case: A Go engineer reviewing code for production deployment ensures all shared resources are correctly initialized, and potential panics are guarded against.

Quick Start

Use the golang-safety skill to review sample Go code snippets and implement best practices to improve code safety.

Frequently Asked Questions about golang-safety

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

FAQPage Schema
What is defensive programming in Go and how does it prevent runtime panics?

Defensive programming in Go prevents runtime panics by implementing safeguards against nil pointer dereferences, unsafe type assertions, and improper resource management. It ensures code robustness by validating operations before execution.

How do I safely handle nil maps and slices in Go to avoid panics?

To safely handle nil maps and slices in Go, always initialize them before writing or iterating. This prevents panic conditions caused by nil pointer dereferences and ensures proper memory safety during concurrent operations.

What's the best way to manage resource cleanup inside loops using defer in Go?

The best way to manage resource cleanup inside loops using defer in Go is to wrap the loop body in a function. This ensures resources like files are closed immediately per iteration rather than accumulating until the outer function returns.

How do I perform safe type conversions and assertions in Go?

Safe type conversions in Go require checking for silent truncation and overflow. For type assertions, always use the comma-ok idiom to prevent panics when the underlying type does not match the expected interface.

Does Go concurrency safety require specific primitives for shared map access?

Go concurrency safety requires synchronization primitives like mutexes for shared map access. Standard Go maps are not safe for concurrent read and write operations, requiring these primitives to prevent runtime panics.