🧩 Building a Multi-Datasource Go Application
In many enterprise systems, you may need to work with multiple databases — for example, customer data in PostgreSQL, legacy records in Oracle, and transactional data in MySQL. Managing these connections efficiently while keeping the code clean can be challenging.
The Multi-Datasource Go project — a Go-based demo showing how to connect to MySQL, PostgreSQL, and Oracle XE within one modular application.
⚙️ Project Overview
The project follows a clean architecture pattern, ensuring clear separation between layers:
- Handlers → handle HTTP routes using Gin
- Services → contain business logic
- Repositories → handle database operations per datasource
- Models → define entity structures
Each datasource is completely independent, configured through a simple YAML file, and initialized at runtime with connection pooling.
🧱 Features
🔌 Multiple database connections (MySQL, PostgreSQL, Oracle)
🌐 RESTful API with Gin
🧠 Domain-driven design with repository pattern
📝 Configurable via YAML and Viper
🐳 Docker Compose setup for quick local testing
⚡ Built-in health checks and auto table creation
🏗️ Clean architecture (domain, repository, service layers)
🧰 Tech Stack
Layer Technology
Web Framework Gin
Configuration Viper
Databases MySQL • PostgreSQL • Oracle XE
Drivers go-sql-driver/mysql, pgx, go-ora
Environment Go 1.25.1 • Docker Compose
🗂️ Project Structure
multi-datasource-go/ ├─ cmd/api/main.go # Application entry point ├─ internal/config # Configuration loader (Viper) ├─ internal/db # Database connection pools ├─ internal/http # API routes and handlers ├─ internal/domain # Entities, repos, services └─ internal/repo # Database-specific repositories
🚀 Getting Started
1. Clone the Repository
git clone https://github.com/HenryXiloj/golang-demos.git
cd golang-demos/multi-datasource-go
2. Start Databases
docker compose -f docker-compose-multiple-db.yml up -d
3. Run the Application
go run cmd/api/main.go
🧪 Test the APIs
# MySQL
curl -X POST http://localhost:9000/api/v1/users \
-H "Content-Type: application/json" \
-d '{"name":"Henry","lastName":"x"}'
# PostgreSQL
curl -X POST http://localhost:9000/api/v2/companies \
-H "Content-Type: application/json" \
-d '{"name":"Test"}'
# Oracle
curl -X POST http://localhost:9000/api/v3/brands \
-H "Content-Type: application/json" \
-d '{"name":"Acme"}'
You’ll see all three databases responding independently with auto-created tables.
You can explore the full source code here:👉 multi-datasource-go
.png)