https://github.com/yuraxdrumz/ports-and-adapters-golang
an example ports and adapters architecture in golang
https://github.com/yuraxdrumz/ports-and-adapters-golang
blog golang hexagonal ports-and-adapters
Last synced: 3 months ago
JSON representation
an example ports and adapters architecture in golang
- Host: GitHub
- URL: https://github.com/yuraxdrumz/ports-and-adapters-golang
- Owner: yuraxdrumz
- Created: 2020-01-31T15:59:56.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-01T16:22:00.000Z (over 5 years ago)
- Last Synced: 2025-07-08T15:04:20.764Z (3 months ago)
- Topics: blog, golang, hexagonal, ports-and-adapters
- Language: Go
- Size: 11.7 KB
- Stars: 20
- Watchers: 0
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Ports And Adapters Golang
An example project of Golang + Ports and Adapters structure on top of https://github.com/golang-standards/project-layout and https://github.com/yuraxdrumz/golang-starter-kit
## Getting Started
1. git clone `git@github.com/yuraxdrumz/ports-and-adapters-golang`
2. Create database folder project's root for sqlite adapter
2. go mod init
3. go run main.go
### PrerequisitesPorts and Adapters divides your code to 3 parts:
- Business-logic - These are your business rules + types, implemented without any dependency on 3rd party modules (self-contained)
- Ports - Interfaces to speak with your business rules
- Adapters - Implementations of the ports, There are two kinds of adapters:
- In(Driver) - your external API to the world. For example - `internal/pkg/adapters/in/http.go`
- Out(Driven) - what your business logic uses. For example - `internal/pkg/adapters/out/reverser/in-memory.go`Usually, you divide `ports` and `adapters` to separate directories, but the best practice in golang is to keep structs near implementations. That is why, I decided to add `ports.go` near each adapter.
Adding a new business logic:
1. Create appropriate ports in `ports.go` file under `internal/app/your-use-case/ports.go`
2. Create your use-case with your application specific logic under `internal/app/your-use-case/logic.go`
3. Create your in/out adapter, for example - `Repository(out)` or `gRPC(in)`. `internal/pkg/adapters/*`
4. Tests!!! `your-file-name_test.go` under same directory as file `internal/app/your-use-case/logic_test.go`