Skip to content

prem9528/goTutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

goTutorial

Simple Gin + GORM example project that provides CRUD APIs for Post resources.

What this repo contains

  • A small REST API built with gin-gonic and gorm.
  • Database initialization in initilizers/ (loads .env, connects via GORM).
  • CRUD controllers in controllers/ (create, list, show, update, delete posts).
  • A simple models.Post type using gorm.Model for timestamps.
  • A migrate folder (migration helpers) and a .env for local configuration.

Project structure (important files)

  • main.go — application entry and route definitions.
  • controllers/controllers.go — handlers for the Post endpoints.
  • models/models.goPost model definition.
  • initilizers/dbinit.go — GORM initialization (reads DB_URL from .env).
  • .env — local env values (not recommended to commit secrets).

Quickstart (local)

Prerequisites

  • Go (1.18+ recommended)
  • A MySQL-compatible server (local MySQL / remote service)
  • git to clone the repo
  1. Clone the repo
git clone <repo-url>
cd goTutorial
  1. Copy .env.example to .env and fill values (do NOT commit .env)
copy .env.example .env
# then edit .env with your DB credentials
notepad .env

Example .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"
  1. Download dependencies
go mod download
  1. (Optional) Run DB migrations if you have migration code:
# Example based on your repo structure
go run .\migrate\migrate.go
  1. Run the app
# Simple run
go run .
# Or build and run
go build -o main.exe
.
  1. (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"

Environment & safety notes

  • Do not commit .env with real credentials. Use .env.example as template.
  • If you publish this repo, rotate any credentials that were committed earlier.

APIs

See docs/API.md for endpoint listing, sample requests and example JSON.

What I changed / added (for the repo owner)

  • 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 .gitignore rules (exclude main.exe, .env, etc.)
  • Add a Makefile or scripts for common tasks
  • Add GitHub Actions for CI

Open docs/API.md for endpoint examples.

About

Simple Gin + GORM example project that provides CRUD APIs for `Post` resources.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages