---
title: Define Interfaces at Consumer Side
impact: MEDIUM
impactDescription: promotes decoupling and simplifies testing
tags: go, architecture, interfaces, quality
---

## Define Interfaces at Consumer Side

In Go, interfaces are satisfied implicitly. Do not define interfaces in the same package where the implementation is defined (producer side). Instead, define the interface in the package that requires the dependency (consumer side).

**Incorrect (interface in producer package):**

```go
// package auth
type Service interface {
    Login(user, pass string) (string, error)
}

type serviceImpl struct { ... }
func (s *serviceImpl) Login(u, p string) (string, error) { ... }
```

**Correct (interface in consumer package):**

```go
// package auth (implementation only)
type Service struct { ... }
func (s *Service) Login(u, p string) (string, error) { ... }

// package handler (consumer)
type AuthProvider interface {
    Login(user, pass string) (string, error)
}

func LoginHandler(auth AuthProvider) { ... }
```

**Benefits:**
- Prevents package circular dependencies
- Allows packages to define exactly what they need from a dependency
- Makes it easier to mock dependencies for unit testing
- Keeps the system more loosely coupled

**Tools:** Code Review, Architecture rules
