Simple Gin + GORM example project that provides CRUD APIs for Post resources.
What this repo contains
- A small REST API built with
gin-gonicandgorm. - Database initialization in
initilizers/(loads.env, connects via GORM). - CRUD controllers in
controllers/(create, list, show, update, delete posts). - A simple
models.Posttype usinggorm.Modelfor timestamps. - A
migratefolder (migration helpers) and a.envfor local configuration.
Project structure (important files)
main.go— application entry and route definitions.controllers/controllers.go— handlers for the Post endpoints.models/models.go—Postmodel definition.initilizers/dbinit.go— GORM initialization (readsDB_URLfrom.env)..env— local env values (not recommended to commit secrets).
Prerequisites
- Go (1.18+ recommended)
- A MySQL-compatible server (local MySQL / remote service)
gitto clone the repo
- Clone the repo
git clone <repo-url>
cd goTutorial- Copy
.env.exampleto.envand fill values (do NOT commit.env)
copy .env.example .env
# then edit .env with your DB credentials
notepad .envExample .env DSN (MySQL / GORM):
DB_URL="<DB_USER>:<DB_PASS>@tcp(<DB_HOST>:<DB_PORT>)/<DB_NAME>?charset=utf8mb4&parseTime=True&loc=Local"
PORT=8080
A concrete (example) DSN:
DB_URL="sql12811479:YourPassword@tcp(sql12.freesqldatabase.com:3306)/sql12811479?charset=utf8mb4&parseTime=True&loc=Local"
- Download dependencies
go mod download- (Optional) Run DB migrations if you have migration code:
# Example based on your repo structure
go run .\migrate\migrate.go- Run the app
# Simple run
go run .
# Or build and run
go build -o main.exe
.- (Optional) Install CompileDaemon to auto-rebuild on file change
go install github.com/githubnemo/CompileDaemon@latest
# Add %USERPROFILE%\go\bin to PATH if needed
# Run the watcher from project dir
CompileDaemon -build "go build -o main.exe" -command ".\main.exe"- Do not commit
.envwith real credentials. Use.env.exampleas template. - If you publish this repo, rotate any credentials that were committed earlier.
See docs/API.md for endpoint listing, sample requests and example JSON.
- Created documentation files only:
README.md,docs/API.md, and.env.example. - I did not modify any Go source logic.
If you want, I can also:
- Add
.gitignorerules (excludemain.exe,.env, etc.) - Add a
Makefileorscriptsfor common tasks - Add GitHub Actions for CI
Open docs/API.md for endpoint examples.