golang-structs-interfaces

Guide Go type design with interfaces, structs, and dependency injection patterns.

2.9k|191|Updated Mar 21, 2026
One-click install
npx skills add https://github.com/samber/cc-skills-golang --skill golang-structs-interfaces
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-structs-interfaces
Source: https://github.com/samber/cc-skills-golang/tree/main/skills/golang-structs-interfaces
Command: npx skills add https://github.com/samber/cc-skills-golang --skill golang-structs-interfaces

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Designing Go types can be challenging when balancing small interfaces, embedding decisions, and clear contracts. This Skill provides guidance on when to use interfaces, how to compose types, and how to apply DI patterns for testable code.

Core Features & Use Cases

  • Principles for minimal interfaces (1-3 methods) and define them where consumed.
  • Strategies for embedding vs named fields, and pointer vs value receivers.
  • Dependency injection via interfaces and compile-time interface checks.
  • Use with common patterns like type assertions and type switches to keep code safe and expressive.

Quick Start

Describe a minimal Go example with a consumer interface, a struct that depends on it, and a concrete implementation.

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 Go interfaces and structs for clean, testable code?

When designing Go types, choose struct embedding to promote methods and satisfy interfaces implicitly, but use named fields when you need to prevent external callers from overriding embedded state or invoking embedded methods directly.

What's the best way to use dependency injection with Go interfaces?

Use pointer receivers when struct methods need to mutate state or handle large structs efficiently, and use value receivers to protect struct immutability or when working with small, copy-safe types.

Why does Go recommend avoiding unnecessary interfaces for structs?

Use type assertions and type switches to extract dynamic behavior from interfaces when needed, ensuring safe execution by checking the `ok` return value or using the comma-ok pattern to prevent runtime panics during type conversion.

When do I need interface segregation in Golang type design?

You need interface segregation in Golang when consumers are forced to depend on methods they do not use; splitting large interfaces into smaller, consumer-specific contracts prevents unused method dependencies and simplifies mocking.