Small Gin + GORM (SQLite) sample project with a simple user lookup API.
- Manually build a Gin + GORM project using global variables.
- Refactor to manual dependency wiring (no globals).
- Use a simple
wirepackage to centralize construction and injection.
- Create core providers
core.NewDB()opens SQLite and runs migrations.core.NewLogger()builds the app logger.
- Create services
service.NewUserService(db, logger)depends on DB + logger.
- Create handlers
api.NewUserApi(userService)depends on the service.
- Create router
router.NewRouter(userApi)registers routes with handlers.
- Start server
main.gocallswire.InitWire()and thenRun().
This keeps dependencies explicit and avoids package-level globals.
This project uses a small manual wiring function (not Google Wire).
wire.InitWire()is a composition root.- It constructs dependencies in order (DB → Logger → Service → API → Router).
- It passes instances down the chain so each layer receives what it needs.
Flow:
NewDB → NewLogger → NewUserService → NewUserApi → NewRouter
- Go 1.25+
go mod tidygo run main.goServer starts on :80.
- GET
/api/user?id=<id>
Example:
curl "http://localhost:80/api/user?id=1"api/ HTTP handlers
core/ DB and logger setup
model/ GORM models
router/ Gin routes
service/ Business logic
wire/ Manual wiring/initialization
- SQLite database file:
db.test - If a user ID does not exist, the API returns
404.