golang-structs-interfaces

Guides Go struct and interface design covering embedding, type assertions, and receiver selection.

1|Updated Feb 2, 2026
One-click install
npx skills add https://github.com/rockcookies/skills --skill golang-structs-interfaces-rockcookies
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-structs-interfaces
Source: https://github.com/rockcookies/skills/tree/main/skills/samber-golang/golang-structs-interfaces
Command: npx skills add https://github.com/rockcookies/skills --skill golang-structs-interfaces-rockcookies

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Go developers often create oversized interfaces, define them in the wrong package, return interfaces from constructors, or panic on bare type assertions. This Skill encodes idiomatic Go type-system rules so your structs and interfaces stay small, composable, testable, and panic-free. ## Core Features & Use Cases - Interface Design Rules: Enforces small 1-3 method interfaces defined where consumed, the "accept interfaces, return structs" principle, and avoiding premature interfaces with a single implementation. - Type Safety Patterns: Covers comma-ok type assertions, type switches, compile-time interface checks (var _ io.ReadWriter = (*T)(nil)), generics over any, and the noCopy sentinel for uncopyable structs. - Struct Design Guidance: Explains embedding vs named fields, struct field tags for JSON/YAML/DB serialization, useful zero values with lazy initialization, and pointer vs value receiver consistency. - Use Case: When reviewing a Go event dispatcher that uses bare type assertions like e.Payload.(*UserCreated), the Skill directs you to switch to comma-ok form and return errors instead of panicking. ## Quick Start Ask the AI to review your Go package's interface and struct design, for example: "Review my Go service — should NewUserService return an interface, and where should I define the UserStore interface?"

Frequently Asked Questions about golang-structs-interfaces

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

FAQPage Schema
Where should I define interfaces in a Go project?▼

Define interfaces where they are consumed, not where they are implemented. The consumer package declares only the methods it needs, keeping it in control of the contract and avoiding imports of a package just for its interface.

Should Go constructors return interfaces or structs?▼

Constructors should return concrete types, never interfaces. Accept interfaces as parameters for flexibility, but return the concrete struct so callers get full access to its fields and methods and can assign it to an interface themselves if needed.

When should I use struct embedding vs a named field in Go?▼

Embed when you want to promote the inner type's full API, meaning the outer type "is a" enhanced version. Use a named field when the inner type is only needed internally, since embedding exposes all of its methods to callers.

How do I prevent a Go struct from being copied?▼

Embed a noCopy sentinel struct with empty Lock and Unlock methods, the same technique used by sync.Mutex and sync.WaitGroup. The go vet tool then flags any accidental copies, and the struct should always be passed by pointer.

Should I use generics or any/interface{} in Go?▼

Prefer generics with constraints like [T comparable] for type-safe operations since Go 1.18. Reserve any for true boundaries where the type is genuinely unknown, such as JSON decoding or reflection.

Can I mix pointer and value receivers on the same Go type?▼

No, receiver types should be consistent across all methods of a type. If any method mutates the receiver and needs a pointer, all methods should use pointer receivers, since method sets differ for T and *T and affect interface satisfaction.