All posts
goarchitecture

Clean Architecture in Go, Without the Ceremony

Layers and boundaries that pay off in real services, and the parts you can skip.

Muhamad Choirul Adam 2 min readPhoto: Unsplash

Clean architecture has a reputation for folders named usecase, interactor, and presenter, and for a lot of interfaces with exactly one implementation. You don't need most of that. You need one idea: dependencies point inward.

The one rule that matters

Your business logic should not know about HTTP, SQL, or the framework you picked this year. The outer layers depend on the inner ones, never the other way around.

Rendering diagram…

A small, honest layout

internal/
  order/
    order.go        // domain types and rules
    service.go      // use cases
    repository.go   // interface the service needs
    postgres.go     // implementation of that interface
    http.go         // handlers

Grouping by feature, not by layer, keeps related code close. The layering still exists; it just lives inside each package.

Define interfaces where they're used

In Go, the consumer owns the interface. The service declares exactly what it needs, and nothing more:

service.go
package order
 
import "context"
 
type Repository interface {
	Create(ctx context.Context, o Order) error
	ByID(ctx context.Context, id string) (Order, error)
}
 
type Service struct {
	repo Repository
}
 
func (s *Service) Place(ctx context.Context, o Order) error {
	if err := o.Validate(); err != nil {
		return err
	}
	return s.repo.Create(ctx, o)
}

The Postgres implementation satisfies this implicitly. Tests can pass a tiny in-memory fake without a mocking library.

What you can skip

  • One interface per struct. Add an interface when there's a second implementation or a test that needs it.
  • Mapping between identical structs. If the HTTP shape equals the domain shape today, share it. Split when they diverge.
  • A use-case object per action. A service with a few methods reads better than ten one-method types.

Why it pays off

When the database changes, only postgres.go changes. When a new transport arrives, say gRPC next to HTTP, the service doesn't notice. That's the whole point: the expensive parts of the system stop being entangled with the cheap ones.