golang-structs-interfaces

Design small Go interfaces and concrete types with compile-time checks.

Updated Oct 1, 2024
One-click install
npx skills add https://github.com/mrlorentx/.files --skill golang-structs-interfaces-mrlorentx
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-structs-interfaces
Source: https://github.com/mrlorentx/.files/tree/main/ai/claude/skills/golang-structs-interfaces
Command: npx skills add https://github.com/mrlorentx/.files --skill golang-structs-interfaces-mrlorentx

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Go developers often struggle to design clean, focused interfaces and concrete types that are easy to test and maintain. This skill provides patterns for small interfaces, proper embedding, and clear decisions between pointer and value receivers, helping you build robust Go type systems.

Core Features & Use Cases

  • Small interfaces (1-3 methods) that are consumed by concrete types.
  • Composition through embedding and interface segregation to keep APIs focused.
  • Dependency injection via interfaces and placing interfaces where they are consumed.
  • Guidance on pointer vs value receivers and struct tags for JSON/YAML/DB serialization.
  • Compile-time interface checks and safe type assertions.

Quick Start

Provide a concrete Go example showing how to define a small interface and a concrete type implementing it.

Frequently Asked Questions about golang-structs-interfaces

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

FAQPage Schema
How do I design small Go interfaces for better testability?

Design small Go interfaces by limiting them to 1-3 methods and placing them where they are consumed. This approach ensures focused APIs, simplifies mocking, and improves overall code testability.

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

Choose pointer receivers when modifying struct state or passing large structs, and value receivers for immutability or small structs. Consistent receiver types ensure clear Go type semantics and prevent method set compilation errors.

How do I implement dependency injection using Go interfaces?

Implement dependency injection in Go by defining small interfaces at the consumer level and passing them as struct fields. This pattern decouples modules and makes unit testing straightforward.

What is the best way to use struct tags for JSON and YAML serialization in Go?

Use idiomatic struct tags by annotating fields with encoding keys like `json:"field_name"`. This maps Go struct fields directly to serialization formats, ensuring correct data parsing and export.

How do compile-time interface checks and type assertions work in Go?

Compile-time interface checks use `var _ Interface = (*Type)(nil)` to guarantee struct compliance. Safe type assertions with the comma-ok pattern prevent runtime panics when extracting specific types from Go interfaces.