This is an example implementation of an E-Commerce basket using Uncle Bobs' Clean Architecture written in Go.
Source: https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
Simple Model:
With adapted Basket:
The golang project structure is based on http://github.com/golang-standards/project-layout.
All code, except the cmd entrypoint, is "hidden" inside the internal directory.
Using a domain-driven design approach, the domains are separated inside the internal/domain directory.
The high-level layers "Entities" and "Use Cases" are combined inside the business directory.
The low-level layers "Adapters" and "Drivers" are separated.
The entities are stored inside this layer.
Also, there are Factory classes to create new entities and Repository classes to retrieve entities from the data layer and save entities into the data layer.
The use cases are stored inside this layer.
Every Use Case has a separate class which improves the readability and understandability.
Also, there are additional Service classes containing more business logic.
And for the output, there are some "Data Transfer Object" (DTO) classes.
The interface adapters are stored inside this layer.
The implemented adapters are a full REST API and a web adapters, but only for the "Show Basket" use case.
The drivers are stored inside this layer.
The implemented drivers are an in-memory driver, but for the basket there is also a MongoDB driver.
go run ./cmd/serverFirst build the docker image:
./docker-build.shThen run the docker container using the built image:
./docker-run.shThe web implementation only shows the basket. (first use case)
To view it, open http://localhost:8080/ in your web browser.
If you want to interact with the basket, please use the REST API described in the following section.
The REST API fully implements all basket use cases with the following routes:
GET /basket
POST /basket/:productId
POST /basket/:productId/:count
PATCH /basket/:productId/:count
DELETE /basket/:productId
DELETE /basketIf you use curl in the shell, you can use jq to prettify the output.
curl http://localhost:8080/basketcurl -XPOST http://localhost:8080/basket/A12345curl -XPOST http://localhost:8080/basket/A12345/2curl -XPATCH http://localhost:8080/basket/A12345/10curl -XPOST http://localhost:8080/basket/A12346/1curl -XDELETE http://localhost:8080/basket/A12346curl -XDELETE http://localhost:8080/basketThe diagrams are built using plantuml.
To recreate them, just run:
./recreate-diagrams.sh