golang-structs-interfaces

Guide Go struct and interface design with composition and type-safety rules.

1|Updated May 26, 2026
One-click install
npx skills add https://github.com/tokiou/caba-inseguridad-v2-be --skill golang-structs-interfaces-tokiou
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-structs-interfaces
Source: https://github.com/tokiou/caba-inseguridad-v2-be/tree/main/.claude/skills/golang-structs-interfaces
Command: npx skills add https://github.com/tokiou/caba-inseguridad-v2-be --skill golang-structs-interfaces-tokiou

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It helps you design Go structs and interfaces that stay maintainable, testable, and correct—avoiding common pitfalls like bloated interfaces, premature abstractions, unsafe type assertions, and broken zero values.

Core Features & Use Cases

  • Small, composable interfaces: Prefer 1–3 method interfaces and compose larger contracts from smaller ones.
  • Interface ownership and DI: Define interfaces where they are consumed, and inject dependencies via interfaces in constructors.
  • Practical correctness patterns: Return concrete types from constructors, make the zero value useful (lazy init), and verify interfaces at compile time.
  • Safe runtime behavior: Use comma-ok type assertions and type switches; use type assertions for optional capabilities (e.g., flush when supported).
  • Struct composition rules: Use embedding to promote an inner type’s API when appropriate, otherwise use named fields to keep dependencies internal.
  • Receiver and copying safety: Keep pointer/value receiver choices consistent and prevent accidental copying with the noCopy sentinel.
  • Type-safety guidance: Prefer generics over any/interface{} except at true boundaries like reflection/JSON decoding.

Quick Start

Use the golang-structs-interfaces skill when you are defining Go structs or interfaces and want guidance on composition, dependency injection, serialization tags, safe assertions, and pointer vs value receiver design.

Frequently Asked Questions about golang-structs-interfaces

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

FAQPage Schema
How do I design small, composable Go interfaces for dependency injection?

To design composable Go interfaces for dependency injection, define small interfaces with 1–3 methods at the consumption site, and inject dependencies via interfaces in constructors to keep your code decoupled and testable.

What's the best way to make a Go struct's zero value useful?

To make a Go struct's zero value useful, utilize lazy initialization within methods so the struct remains safe and functional immediately without requiring an explicit constructor call.

Should I use type assertions or generics for optional capabilities in Go?

For optional capabilities in Go, use comma-ok type assertions to check for additional methods safely, and prefer generics over any or interface{} except at true boundaries like JSON decoding or reflection.

When should I use embedding versus named fields in Go struct design?

Use Go struct embedding to promote an inner type's API when you want to expose its methods, otherwise use named fields to keep dependencies internal and prevent unwanted API surface expansion.

How do I prevent accidental copying of a Go struct?

To prevent accidental copying of a Go struct, embed a noCopy sentinel zero value and ensure consistent pointer or value receiver selection across all methods to maintain safe copying behavior.

Why return concrete types from Go constructors instead of interfaces?

Returning concrete types from Go constructors provides callers full access to the struct's methods while still satisfying interfaces, and compile-time interface checks verify correctness without premature abstraction.