Golang Clean Code Architecture Template
Go (Golang) Clean Architecture based on Reading Uncle Bob's Clean Architecture. This code using GoFiber, GORM and User registerm authentication feature.
Installation
- Replace "github.com/max38/golang-clean-code-architecture" with "your module name"
- Copy or Change "env.template" file name to ".env" or ".env.dev"
- Check on code for loading config in these files
src/infrastructure/database/postgres/migration/main.go
src/infrastructure/gofiber/main.go
- Run Postgresql
- Migrate RDBMS
- Start web project
run postgresql on docker
docker run -p 5432:5432 --name postgres-golang-first-api -e POSTGRES_PASSWORD=golang-first-api -d postgres:15
or docker compose
docker-compose up
Migrate RDBMS
go run src/infrastructure/database/postgres/migration/main.go
start web project (Production)
go run src/infrastructure/gofiber/main.go
Template for every projdct
Project Structure
src
|-config
|-domain
|-entities
|-models
|-repositories
|-infrastructure
|-aws_lambda
|-database
|-gofiber
|-interface
|-handlers
|-gofiber
|-repositories
|-postgres
|-shared
|-usecases
Install lib
REST API Lib
go get github.com/gofiber/fiber/v2
Generate Swagger Document
- Install follow this https://github.com/gofiber/swagger
- Go to ChatGPT and prompt with
Please generate Declarative Comments Format for Swagger Doc follow this gofiber code.
"""<Handler Code>"""
-
update Declarative Comments from ChatGPT to Handler
-
Generate swagger.json
swag init -g src/infrastructure/gofiber/main.go -o src/infrastructure/gofiber/docs/
- Go to localhost:3000/swagger/
Configuration Lib
go get github.com/knadh/koanf/v2
go get github.com/knadh/koanf/providers/file
go get github.com/knadh/koanf/providers/env
go get github.com/knadh/koanf/parsers/dotenv
GORM
go get -u gorm.io/gorm
go get -u gorm.io/driver/postgresql
MongoDB
if use mongodb
go get go.mongodb.org/mongo-driver
go get go.mongodb.org/mongo-driver/mongo
For Development
Install Gofiber cli
follow this https://github.com/gofiber/cli
start web project (Development)
fiber dev -t ./src/infrastructure/gofiber/main.go
Add More Model
- Define in src/domain/models/xxxx.go
- Add Model at src/config/crudModels.go