go-composition

Create reusable and maintainable designs with inheritance of structs and embedding of interfaces in Go.

2|Updated Feb 14, 2025
One-click install
npx skills add https://github.com/gofhir/validator --skill go-composition
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-composition
Source: https://github.com/gofhir/validator/tree/main/.claude/skills/go-composition
Command: npx skills add https://github.com/gofhir/validator --skill go-composition

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill addresses the complexities of extending existing types and combining behaviors in Go, offering robust solutions for building flexible and maintainable codebases.

Core Features & Use Cases

  • Struct Embedding: Inherit methods and fields from base types to build specialized structures.
  • Interface Embedding: Compose interfaces to create more complex contracts and enable flexible polymorphism.
  • Service Composition: Combine multiple services to achieve a unified functionality, handling fallback scenarios.
  • Decorator Pattern: Enhance existing handlers with additional logic like logging or authentication without modifying their core implementation.

Quick Start

Use the go-composition skill to understand how to embed a BaseValidator struct within a TerminologyValidator struct.

Frequently Asked Questions about go-composition

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

FAQPage Schema
How do I extend an existing Go struct without modifying its original implementation?

Go struct embedding allows you to inherit methods and fields from base types to build specialized structures, extending existing types without direct modification. This approach provides a flexible way to compose behaviors while keeping the codebase maintainable.

What is the best way to combine multiple Go interfaces into a single contract?

Interface embedding in Go composes multiple interfaces into a single complex contract, enabling flexible polymorphism across your application. This pattern allows you to comprehensive behavioral requirements without coupling specific implementations to the contract.

How do I add logging or authentication to a Go handler without changing its core logic?

The decorator pattern in Go enhances existing handlers with additional logic like logging or authentication without modifying their core implementation. This service composition technique wraps handlers to layer supplementary behaviors around the original functionality.

Can I implement behavioral inheritance in Go using composition patterns?

Yes, Go composition patterns facilitate behavioral inheritance by using struct embedding for type extension and interface embedding for flexible contracts. This allows you to combine functionalities and implement fallback scenarios effectively within your services.

When should I use service composition over direct struct embedding in Golang?

Use service composition when you need to combine multiple distinct services to achieve unified functionality and handle fallback scenarios. Struct embedding is better suited for extending a single base type's fields and methods to build a specialized structure.