smithery.ai

accept-interfaces-return-structs

Core pattern for flexible, testable Go APIs

First seen Mar 22, 2026

Installation

$ npx skills add https://smithery.ai

Similar popular skills

Related neighbors and high-traction skills in the same topics — useful to compare before installing.

Also in this package

Other skills from smithery.ai · top by installs.

npx skills add https://smithery.ai

Browse all from smithery.ai

More details

Agent compatibility

Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.

Claude Code Not declared
Cursor Not declared
Codex Not declared
GitHub Copilot Not declared
Windsurf Not declared
Gemini CLI Not declared
Cline Not declared
OpenCode Not declared

Package contents

Files included with this skill beyond the listing page.

  • skill md SKILL.md 1,480 B
  • docs SUMMARY.md 83 B

History

  1. First seen on skills.sh
  2. First recorded snapshot · 1 installs

SKILL.md

Accept Interfaces, Return Structs

The fundamental Go interface design principle: functions should accept interfaces but return concrete types.

The Pattern

CORRECT - Accept interface, return concrete

type Storage interface {
    Save(data []byte) error
}

func NewProcessor(s Storage) *Processor {
    return &Processor{storage: s}
}

func (p *Processor) Process(input string) (*Result, error) {
    // Returns concrete *Result, accepts Storage interface
    return &Result{Value: input}, nil
}

WRONG - Return interface unnecessarily

func NewProcessor(s *FileStorage) Storage {
    // Locks caller into interface, prevents direct method access
    return &Processor{storage: s}
}

Why This Works

Accepting interfaces:

  • Caller controls abstraction
  • Easy to mock/test
  • Flexible composition

Returning concrete types:

  • No hidden behaviors
  • All methods visible
  • Can add methods without breaking compatibility

When to Deviate

Return interface when:

func NewLogger(env string) io.Writer {
    // Valid: stdlib interface, multiple implementations
    if env == "prod" {
        return &fileLogger{}
    }
    return &consoleLogger{}
}

Return interfaces only when:

  • Using stdlib interfaces (io.Writer, io.Reader)
  • Multiple implementations chosen at runtime
  • Interface already well-established in ecosystem