Back to list
JamesPrial

accept-interfaces-return-structs

by JamesPrial

WIP - collection of various Claude stuff i make/use/have_Claude_hallucinate

2🍴 0📅 Jan 23, 2026

SKILL.md


name: accept-interfaces-return-structs description: Core pattern for flexible, testable Go APIs

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

Score

Total Score

65/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

1ヶ月以内に更新

+10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

+5

Reviews

💬

Reviews coming soon