golang-structs-interfaces

Design Go structs and interfaces with composable contracts.

2|Updated Feb 12, 2024
One-click install
npx skills add https://github.com/adibfirman/dotfiles --skill golang-structs-interfaces-adibfirman
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-structs-interfaces
Source: https://github.com/adibfirman/dotfiles/tree/main/claude/.claude/skills/technical/golang/golang-structs-interfaces
Command: npx skills add https://github.com/adibfirman/dotfiles --skill golang-structs-interfaces-adibfirman

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It solves confusion and recurring mistakes when designing Go type systems, especially around how to structure structs and interfaces for clarity, testability, and correct runtime behavior.

Core Features & Use Cases

  • Small, composable interfaces: Prefer 1–3 method interfaces and compose larger contracts from smaller ones.
  • Interfaces defined at the consumer: Define interfaces in the package that needs to consume the dependency, not where it’s implemented.
  • Accept interfaces, return structs: Accept interface parameters for flexibility, but return concrete types from constructors for clarity.
  • Safe and expressive type behavior: Use comma-ok type assertions and type switches, plus optional-capability checks via type assertions.
  • Robust struct design: Make the zero value useful, add field tags for serialization, use compile-time interface checks, keep receiver types consistent, and prevent accidental struct copies with the noCopy sentinel.
  • Pointer vs value receivers and embedding rules: Choose receivers consistently and embed only when you want to promote the full inner API (“is a”), otherwise use named fields (“has a”).

Quick Start

Use the golang-structs-interfaces skill to review a proposed Go API and refactor the structs, interfaces, receivers, and serialization tags to match best practices for safe composition and maintainable type contracts.

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 safe type composition?

Designing Go structs and interfaces for safe composition requires keeping interfaces small with 1–3 methods, defining them at the consumer package, returning concrete types from constructors, and accepting interface parameters to ensure composability and maintainability.

What is the best way to use type assertions in Go to avoid runtime panics?

The best way to use type assertions in Go without causing runtime panics is to use the comma-ok idiom or type switches, which safely check optional capabilities and handle failed assertions gracefully without crashing the program.

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

Use struct embedding in Go when you want to promote the full inner API to create an "is a" relationship, and use named fields for a "has a" relationship when you want to encapsulate the inner type without exposing its entire method set.

How do I make the zero value of a Go struct useful?

To make the zero value of a Go struct useful, design your struct fields so their default initialized state is immediately safe and operational without requiring explicit constructor calls, and add a noCopy sentinel to prevent accidental unsafe struct copies.

Why should Go constructors return concrete types instead of interfaces?

Go constructors should return concrete types instead of interfaces to preserve type clarity and allow callers to access concrete type methods, while accepting interfaces as parameters preserves dependency injection flexibility and simplifies testing.

Does dependency injection in Go require interface type assertions?

Dependency injection in Go often uses interface type assertions to check for optional behaviors, allowing consumers to dynamically verify if an injected dependency supports specific capabilities before attempting to invoke those methods.