golang-structs-interfaces

Design Go types with small interfaces, embedding, and dependency injection.

5|1|Updated Apr 14, 2026
One-click install
npx skills add https://github.com/omarluq/og-template --skill golang-structs-interfaces-omarluq
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-structs-interfaces
Source: https://github.com/omarluq/og-template/tree/main/.agents/skills/golang-structs-interfaces
Command: npx skills add https://github.com/omarluq/og-template --skill golang-structs-interfaces-omarluq

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Go developers often struggle to design clean, composable types and interfaces that are easy to test and maintain. This skill guides you through small, focused interfaces, embedding vs. named fields, dependency injection, and robust receiver usage to build clear, maintainable Go code.

Core Features & Use Cases

  • Interface design principles: keep interfaces small (1-3 methods) and define them where consumed.
  • Constructors & DI: return concrete types from constructors; accept interfaces to enable flexible wiring and easy testing.
  • Composition patterns: choose between embedding and named fields to promote or hide APIs; enforce receiver consistency.
  • Use Case: build a service with a repository interface and a concrete implementation, then test it with a mock.

Quick Start

Provide a minimal Go example that demonstrates a concrete type, a tiny interface, embedding, and a constructor returning the concrete type.

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 maintainable code?

Design Go interfaces by keeping them small with 1-3 methods and defining them where consumed. This approach prevents premature abstractions and ensures your types remain composable, testable, and easy to maintain.

Should Go constructors return concrete types or interfaces?

Go constructors should return concrete types rather than interfaces. Accept interfaces as parameters to enable flexible wiring and dependency injection, which simplifies testing with mocks while keeping API boundaries clear.

What is the difference between embedding and named fields in Go structs?

Embedding in Go structs promotes methods and fields to the outer type, exposing the API publicly. Named fields hide the inner API, requiring explicit access, allowing precise control over composition and encapsulation.

When should I avoid premature interfaces in Go?

Avoid premature interfaces in Go until you have multiple implementations requiring abstraction. Define interfaces at the consumer side to prevent unnecessary coupling and favor generics over empty interfaces when possible.

How do I use dependency injection in Go to test services with mocks?

Use dependency injection in Go by defining a repository interface at the service consumer level. Pass a concrete mock implementation into the service constructor to isolate logic and execute reliable unit tests.