golang-structs-interfaces

Design and validate Go type APIs for interface placement and constructor returns.

2|Updated Mar 13, 2023
One-click install
npx skills add https://github.com/haipham22/golang-sample --skill golang-structs-interfaces-haipham22
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-structs-interfaces
Source: https://github.com/haipham22/golang-sample/tree/main/.agents/skills/golang-structs-interfaces
Command: npx skills add https://github.com/haipham22/golang-sample --skill golang-structs-interfaces-haipham22

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps you design clean, maintainable Go types by choosing the right balance between structs and interfaces. It reduces coupling, avoids premature abstraction, and prevents common mistakes that make Go code harder to test, evolve, or reason about.

Core Features & Use Cases

  • Interface placement: Define interfaces where they are consumed, not where they are implemented.
  • Constructor design: Prefer accepting interfaces while returning concrete structs for clarity and flexibility.
  • Type safety patterns: Use safe type assertions, optional capability checks, and compile-time interface verification.
  • Struct design: Improve zero-value usability, choose pointer versus value receivers consistently, and use embedding intentionally.
  • Practical Go patterns: Apply no-copy sentinels, field tags, and generics instead of empty interface types when possible.
  • Use case: Review a service layer, then rewrite its contracts so dependencies are testable, APIs stay concrete, and runtime panics are avoided.

Quick Start

Use this skill to review a Go package and rewrite its structs, interfaces, constructors, and receiver choices according to the listed design rules.

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 Go interfaces to reduce coupling and avoid premature abstraction?

Define Go interfaces where they are consumed rather than where they are implemented to keep them small and consumer-defined. This prevents unnecessary coupling and makes application code easier to test, evolve, and reason about.

What is the best way to structure Go constructors for type safety and testability?

Structure Go constructors by accepting interfaces as parameters while returning concrete struct types. This design ensures clarity, maintains flexibility for dependency injection, and avoids runtime panics during testing.

Should I use pointer or value receivers for Go structs?

Choose pointer versus value receivers consistently across all methods of a Go struct. Consistent receiver selection improves zero-value usability, prevents vet-friendly memory safety issues, and ensures safe type handling.

When do I need to use compile-time interface checks in Go?

Use compile-time interface checks in Go when you want to verify type API compliance and enforce safe type assertions before runtime. This prevents runtime panics and ensures optional capability checks are validated early.

How do I apply generics instead of empty interface types in Go?

Apply Go generics to replace empty interface types when you need type-safe abstractions without losing compile-time guarantees. This approach enforces safe type handling and eliminates the need for risky runtime comma-ok assertions.

Why use no-copy sentinels and struct embedding in Go application code?

Use no-copy sentinels and intentional struct embedding in Go to enforce memory safety conventions and prevent accidental value copying. These patterns ensure safe type handling and maintain clear structural composition within application code.