Hexagonal architecture, also called ports and adapters, separates an application’s core rules from the systems it talks to. In Go, interfaces can define the ports through which handlers, databases, message queues, and external APIs interact with the core.
Separate responsibilities
- Core or service: coordinates application behavior and business rules.
- Ports: interfaces that describe required operations, such as loading or saving a record.
- Adapters: concrete implementations for HTTP, SQL, files, or other external systems.
- Handlers: translate transport requests into application calls and format responses.
A common Go project might organize domain types, service logic, repository interfaces, database adapters, and HTTP handlers in separate packages. The exact directory names are less important than keeping dependencies pointed inward: the core should not need to import a particular database driver or web framework.
Testing through interfaces
A service can be tested with a small in-memory or mock repository that implements the same interface as the database adapter. Handler tests can use a test service or fake dependencies. This makes focused tests easier, while integration tests still verify the real database or transport wiring.
Interfaces are useful when they clarify a boundary; adding one for every type can make a small program harder to follow. Adapt the pattern to the size and needs of the application, and keep error handling and transaction behavior explicit.
Contact
For questions about the site, use the contact option below.
