golang-structs-interfaces

Guides Go struct and interface design covering embedding, type assertions, field tags, and receiver choices.

Updated Jun 15, 2026
One-click install
npx skills add https://github.com/2877389577/novels_ai_gen --skill golang-structs-interfaces-2877389577
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-structs-interfaces
Source: https://github.com/2877389577/novels_ai_gen/tree/main/.agents/skills/golang-structs-interfaces
Command: npx skills add https://github.com/2877389577/novels_ai_gen --skill golang-structs-interfaces-2877389577

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 an AI coding agent designs small composable interfaces, useful zero values, and consistent receivers from the start. ## Core Features & Use Cases - Interface design rules: keep interfaces to 1-3 methods, define them where consumed, accept interfaces and return concrete structs, and avoid premature abstraction. - Type safety patterns: comma-ok type assertions, type switches, compile-time interface checks with var _ Interface = (*Type)(nil), and generics over any. - Struct design guidance: struct vs interface embedding, field tags for JSON/YAML/DB serialization, pointer vs value receiver consistency, and the noCopy sentinel for uncopyable structs. - Use Case: When asked to design a notification service depending on an email client, the agent defines a small Sender interface in the consumer package, returns a concrete struct from the constructor, and injects the dependency via the interface for easy mocking in tests. ## Quick Start Ask the agent to design a Go service with its dependencies and data types, for example: design a UserService with a UserStore dependency following idiomatic Go interface and struct patterns.

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 Go?▼

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

Should Go constructors return interfaces or structs?▼

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

When should I use pointer vs value receivers in Go?▼

Use pointer receivers when the method mutates the receiver, the struct is large, or it contains a sync.Mutex. Keep receiver types consistent across all methods of a type; if one method needs a pointer receiver, all should use pointers.

How do I check a Go type implements an interface at compile time?▼

Add a blank identifier assignment near the type definition: var _ io.ReadWriter = (*MyBuffer)(nil). It costs nothing at runtime and fails the build immediately if the type stops satisfying the interface.

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

Embed when the outer type should promote the inner type's full API, an 'is a' relationship. Use a named field when the dependency is internal only, a 'has a' relationship, so its methods stay hidden from callers.

When should I use generics instead of any in Go?▼

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