Vjerci.com

A personal blog by software engineer

January, 6-th, 2026

Clean Architecture -> Independence of external agencies


Golang clean architecture
1. Separation of concerns 2. Dependency rule 3. Testability 4. Independence of external agencies 5. Tracing

This tutorial series assumes you are familiar with golang, as we will be using it thorough this article series.


What is independence of external agencies

independence of external agencies rule means to keep your codebase loosely coupled from external services, APIs, or third-party systems

Basically what you want to do is depend on interfaces, not implementations.

What “independence” means

You want your core logic to:

  1. Not depend directly on external APIs, databases, or services
  2. Be easy to test without real external calls
  3. Allow swapping implementations (e.g., switch Stripe → PayPal, or mock in tests)

Example

Your business logic should depend on interface and not on implementation.

Lets say we want to include stripe payment processor for our Orders.

This would be a good example on how to do it:

	type PaymentProcessor interface {
		Charge(amount int) error
	}

	type OrderService struct {
		payment PaymentProcessor
	}

	func (s *OrderService) Checkout(amount int) error {
		return s.payment.Charge(amount)
	}

Than instead of calling stripe directly from a service you would implement your interface:

	type StripeClient struct{}

	func (s *StripeClient) Charge(amount int) error {
		// call Stripe API here
		return nil
	}

Now stripe is just a single implementation that you inject:

	service := OrderService{
		payment: &StripeClient{},
	}

By doing interfaces, you enable yourself to easily mock it during tests:

	type MockPayment struct{}

	func (m *MockPayment) Charge(amount int) error {
		return nil
	}

Why it matters

What are the benefits of coding it this way:

  1. It is easier to test (no real API calls)
  2. Safer refactoring, you refractor only payment provider without touching other systems
  3. Swappable providers, you can easily go from stripe to paypal or vise versa
  4. It is better long-term maintainability. It is just easier to reason about it

Summary

We’ve learned a bit how to handle external api calls. We’ve also learned about best practices about them - handle them through interfaces. We’ve also learned why it matters.