← ClaudeAtlas

go-interfaceslisted

Enforces Go interface design — interfaces defined by the consumer, kept to one or two methods, accepted as parameters while structs are returned, and never created before a second implementation exists. Use when writing or reviewing Go abstractions, mocks, or package boundaries, and when the user mentions interface design, mocking, dependency injection, "accept interfaces return structs", io.Reader, io.Writer, generics vs interfaces, or asks "should this be an interface", "how do I test this dependency".
CasLubbers/code-design-skills · ★ 1 · Code & Development · score 62
Install: claude install-skill CasLubbers/code-design-skills
# Go interfaces Go interfaces are satisfied implicitly. The implementing type never names the interface, which flips where the interface belongs. ## The consumer declares the interface Define it in the package that *uses* it, listing only what that package calls. ```go // package notify — the consumer. It needs exactly one method. type UserFinder interface { FindUser(ctx context.Context, id string) (*User, error) } func Send(ctx context.Context, f UserFinder, id string) error { ... } ``` ```go // package store — the producer. Returns a concrete type, declares no interface. func New(db *sql.DB) *Store { ... } func (s *Store) FindUser(ctx context.Context, id string) (*User, error) { ... } ``` `*store.Store` satisfies `notify.UserFinder` with no import between them and no declaration linking them. Producer-side interfaces (`store.StoreInterface` next to `store.Store`) invert this: they force every consumer to depend on a surface far wider than it uses, and they change whenever any consumer needs something new. ## Keep them small One or two methods. The standard library's most reused interfaces have one: ```go type Reader interface { Read(p []byte) (n int, err error) } type Writer interface { Write(p []byte) (n int, err error) } type Stringer interface { String() string } ``` A large interface is not reusable, not implementable in a test without a pile of stubs, and usually a struct wearing a disguise. If yours has seven methods, you have described a type, not a ca