go-structs-interfaces

Designs and audits Go structs and interfaces using composition, embedding, and receiver conventions.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill go-structs-interfaces-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-structs-interfaces
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/go/go-structs-interfaces
Command: npx skills add https://github.com/asarchami/dotfiles --skill go-structs-interfaces-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Go developers often over-engineer type hierarchies with large interfaces, premature abstractions, and inconsistent receiver choices, leading to rigid code that is hard to test and maintain. This Skill provides concrete rules for composing small interfaces, embedding structs, and choosing pointer versus value receivers. ## Core Features & Use Cases - Type Design Workflow: Step-by-step guidance for designing new Go types, from starting concrete to extracting interfaces only when a second implementation or testability requirement appears. - Design Auditing: A review checklist that flags oversized interfaces, bare type assertions, missing field tags, mixed receiver styles, and non-copyable structs lacking noCopy guards. - Reference Tables: Quick lookups for standard library interfaces, field tag directives (json, db, yaml, xml, validate), and pointer versus value receiver decision criteria. - Use Case: When building a UserService that depends on a database, define a small UserStore interface in the consumer package, return the concrete *UserService from its constructor, and add a compile-time check so tests can inject mocks without a real database. ## Quick Start Ask the assistant to review your Go package's type design using the go-structs-interfaces guidelines and fix any oversized interfaces or bare type assertions.

Frequently Asked Questions about go-structs-interfaces

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

FAQPage Schema
How do I design interfaces in Go?

Keep Go interfaces small at one to three methods and define them in the package that consumes them, not the implementor's package. Start with concrete types and extract an interface only when a second implementation or a testability requirement appears.

When should I use pointer vs value receivers in Go?

Use pointer receivers when the method mutates the receiver, the struct contains a sync.Mutex, or the struct is large. Use value receivers for small immutable types, and keep the style consistent across all methods of a type.

Should Go constructors return interfaces or structs?

Constructors should accept interfaces as parameters where flexibility matters but return concrete types. Returning concrete types gives callers full access to the type's fields and methods, while interface parameters enable dependency injection and mocking in tests.

How do I check a type satisfies an interface at compile time?

Add a declaration like var _ io.ReadWriter = (*MyBuffer)(nil) near the type definition. The build fails immediately if the type stops satisfying the interface, and it costs nothing at runtime.

When should I embed a struct versus use a named field in Go?

Embed when the outer type should promote the inner type's full API, meaning it is an enhanced version of it. Use a named field when the inner type is an internal dependency that should not leak its methods to callers.

Why does my Go struct panic with a nil map at runtime?

Structs with map or slice fields are nil at their zero value, so writing to them panics. Guard with lazy initialization inside methods, checking if the map is nil and allocating it before use, so the zero value stays usable.