golang-structs-interfaces

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

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

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 inconsistently. This Skill encodes idiomatic Go type-system rules so an AI coding agent produces correct, testable struct and interface designs on the first pass. ## 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 with a single implementation. - 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, struct field tags for JSON/YAML/DB serialization, pointer vs value receiver selection, and useful zero values via lazy initialization. - 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 adds a compile-time interface check. ## Quick Start Ask the agent to review your Go package's interfaces and struct designs, for example by requesting it to check whether your constructors return concrete types and your interfaces are defined where consumed.

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. The consumer declares only the methods it needs, keeping the contract under its control and avoiding imports solely for an interface type.

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 retain 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 a method mutates the receiver, the struct is large, or it contains a sync.Mutex. Keep receiver types consistent: if any method on a type uses a pointer receiver, all methods should.

How do I verify 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 avoid creating an interface in Go?▼

Avoid interfaces with only one implementation and no testing need. Start with a concrete struct and extract an interface later when a second implementation or a test mock genuinely requires it.

Why do bare type assertions panic in Go?▼

A bare assertion like s := val.(string) panics at runtime if the dynamic type differs. Always use the comma-ok form s, ok := val.(string) and handle the failure case, especially for deserialized or externally sourced data.