Skip to content

plh97/wire-learn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wire-learn

Small Gin + GORM (SQLite) sample project with a simple user lookup API.

Learn Steps

  1. Manually build a Gin + GORM project using global variables.
  2. Refactor to manual dependency wiring (no globals).
  3. Use a simple wire package to centralize construction and injection.

Manual Wiring (step by step)

  1. Create core providers
  • core.NewDB() opens SQLite and runs migrations.
  • core.NewLogger() builds the app logger.
  1. Create services
  • service.NewUserService(db, logger) depends on DB + logger.
  1. Create handlers
  • api.NewUserApi(userService) depends on the service.
  1. Create router
  • router.NewRouter(userApi) registers routes with handlers.
  1. Start server
  • main.go calls wire.InitWire() and then Run().

This keeps dependencies explicit and avoids package-level globals.

How wire Works Here

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

Requirements

  • Go 1.25+

Install

go mod tidy

Run

go run main.go

Server starts on :80.

API

  • GET /api/user?id=<id>

Example:

curl "http://localhost:80/api/user?id=1"

Project Structure

api/        HTTP handlers
core/       DB and logger setup
model/      GORM models
router/     Gin routes
service/    Business logic
wire/       Manual wiring/initialization

Notes

  • SQLite database file: db.test
  • If a user ID does not exist, the API returns 404.

About

to learn wire step by step

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages