go-interfaces

JamesPrial 的头像作者 JamesPrial

Go 接口设计模式和最佳实践

📖 中文导读:Go 接口设计模式和最佳实践 以下正文为该技能的原始 SKILL.md 内容(英文),上方安装命令可直接使用。

name: go-interfaces description: Go interface design patterns and best practices

Interface Design

Master Go's approach to interfaces: implicit satisfaction, consumer-driven design, and composition patterns.

Route by Concern

  • Accept/return patterns → see design/

    • Accept interfaces, return structs principle
    • When to deviate from the rule
  • Interface pollution → see pollution/

    • Detecting unnecessary abstractions
    • Premature interface creation
  • Embedding patterns → see embedding/

    • Interface composition techniques
    • Method set expansion

Quick Check

  • Interface defined by consumer, not producer
  • Keep interfaces small (<5 methods)
  • Return concrete types from functions
  • No interface{} unless truly necessary