golang-structs-interfaces

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

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Go developers often create oversized interfaces, return interfaces from constructors, panic on bare type assertions, or mix pointer and value receivers, leading to brittle and untestable code. This Skill encodes idiomatic Go type-system rules so an AI coding agent applies them consistently when designing or reviewing structs and interfaces. ## Core Features & Use Cases - Interface Design Rules: Enforces small 1-3 method interfaces defined at the consumer, the accept-interfaces-return-structs principle, and avoiding premature interfaces before a second implementation exists. - Type Safety Patterns: Covers comma-ok type assertions, type switches, compile-time interface checks with var _ Interface = (*Type)(nil), generics over any, and the noCopy sentinel for uncopyable structs. - Struct Mechanics: Guides struct vs interface embedding decisions, struct field tags for JSON/YAML/DB serialization, useful zero values via lazy initialization, and pointer vs value receiver consistency. - Use Case: When asked to design a notification service depending on an email client, the agent defines a minimal Sender interface in the consumer package, returns a concrete struct from the constructor, and injects the dependency for easy mocking in tests. ## Quick Start Ask the agent to design or review Go types, for example: review my Go service struct and tell me whether the interfaces, receivers, and field tags follow idiomatic Go design.

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 in the package that consumes them, not the package that implements them. This keeps the consumer in control of the contract and avoids importing a package just for its interface; the implementor exports a concrete struct.

Should a Go constructor return an interface or a struct?▼

Return a concrete struct type from constructors, never an interface. Accept interfaces as parameters for flexibility, but returning concrete types gives callers full access to fields and methods, and they can still assign the result to an interface variable.

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

Use pointer receivers when the method modifies the receiver, the struct contains a sync.Mutex, or the struct is large. Keep receiver types consistent: if any method on a type uses a pointer receiver, all methods should.

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 the build fails immediately if the type stops satisfying the interface.

When should I embed a struct vs use 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 dependency is internal only, since embedding exposes all methods of the inner type to callers.

Should I use any or generics for type-safe Go functions?▼

Prefer generics with constraints, such as func Contains[T comparable](slice []T, target T) bool, since Go 1.18. Reserve any for true boundaries where the type is genuinely unknown, like JSON decoding or reflection.