This structure, created following the development guide's for vertical slice architecture, will help to isolate the dependencies, make development easier and have a cleaner and testable code in every package.
Clean architecture is an approach to software development where code and functionality are organized around individual features or user stories, encompassing all layers of the application from user interface to data access, promoting autonomy, reduced dependencies, and iterative development.
A brief description of the layout:
.github
has two template files for creating PR and issue. Please see the files for more details..gitignore
varies per project, but all projects need to ignorebin
directory..golangci.yml
is the golangci-lint config file.Makefile
is used to build the project. You need to tweak the variables based on your project.CHANGELOG.md
contains auto-generated changelog information.README.md
is a detailed description of the project.cmd
contains the main.go file that is our starting point to executepkg
places most of project business logic.migrations
contains all vendored code.internal
contains all the api logic.domain layer
: Contains the business logic and entities.service layer
: Contains the application-specific logic and use cases.infrastructure layer:
Deals with external dependencies and frameworks.interface layer:
Handles communication with external systems or UI.
Programming language Go
Framework Fiber
Dependency injection Uber dig
Database Postgre SQL
Container Docker
Live reload Air
POST /api/product
Parameter | Type | Description |
---|---|---|
name |
string |
Required. Product Name |
sku |
string |
Required. Product Sku must be Unique |
category |
string |
Required. Product Category |
price |
float |
Required. Product Price |
├───internal
│ ├───product
│ │ │ port.go
│ │ │ product.go
│ │ │
│ │ ├───handler
│ │ │ createProduct.go
│ │ │ handler.go
│ │ │
│ │ ├───infrastructure
│ │ │ productRepository.go
│ │ │
│ │ ├───mock
│ │ │ mockProductRepository.go
│ │ │
│ │ └───service
│ │ createProduct.go
│ │ service.go
├───internal
│ ├───product
│ │ ├───infrastructure
│ │ │ productRepository.go
├───internal
│ ├───product
│ │ └───service
│ │ createProduct.go
│ │ service.go
├───internal
│ ├───product
│ │ └───service
│ │ createProduct.go
│ │ service.go
├───internal
│ ├───product
│ │ ├───handler
│ │ │ createProduct.go
│ │ │ handler.go
├───internal
│ ├───product
│ │ ├───handler
│ │ │ createProduct.go
│ │ │ handler.go
├───internal
│ ├───product
│ │ ├───mock
│ │ │ mockProductRepository.go
# It uses mockery Run
mockery
├───internal
│ ├───product
│ │ └───service
│ │ createProduct.go
│ │ service.go
├───pkg
│ ├───injection
# Build server
docker-compose -p go-clean-architecture build
# Start server
docker-compose up -d
# Stop server
docker-compose down
# Live reload
air
# To run unit testing
go test ./...
# To run unit testing coverage
go test -cover ./...
# Clean dependencies
go mod tidy
# Run formating
go fmt ./...
# Remove unused imports
goimports -l -w .
# Run linting
golangci-lint run ./...
# Run vetting
go vet ./...
# Run shadow to check shadowed variables
# Install shadow
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest
# Run shadow
shadow ./...
# Install migration to linux dist on wsl
curl -L https://github.com/golang-migrate/migrate/releases/download/v4.17.0/migrate.linux-amd64.tar.gz | tar xvz
mv migrate $GOPATH/bin/
# Migration help
migrate -help
# Create the script
migrate create -ext sql -dir migration -seq [script_name]
# Run the script
migrate -database ${POSTGRESQL_URL} -path migration up
# It will run automatically when the database initializes
# Build server
make build-server
# Start server
make start-server
# Stop server
make stop-server
# Live reload
make live-reload
# To run unit testing
make test
# To run unit testing coverage
make test-coverage
# Clean dependencies
make clean-deps
# Run formating
make format
# Remove unused imports
make clean-imports
# Run linting
make lint
# Run vetting
make vet
# Run shadow to check shadowed variables
# Install shadow
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest
# Run shadow
make check-shadow
# Run vetting to lint, format and vet your once
make lint-format
# Create the script (replace your_script_name with the actual name)
make migrate-create name=your_script_name
# Run the script
make migrate-up
# It will run automatically when the database initializes
To modify/add configuration via environment variables, use the .env
file, which contains basic app configuration.