Go + Gin + GORM REST API
Build a production-ready REST API in Go using the Gin web framework, GORM ORM, PostgreSQL, and Air for hot-reload development.
Initialize Go Module
Create a project directory and initialize a Go module. The module path is used as the import prefix throughout the project.
mkdir my-go-api && cd my-go-api && go mod init github.com/yourusername/my-go-apiInstall Air for Hot Reload
Air watches your Go files and recompiles automatically on change — the equivalent of ts-node-dev in the Go world.
go install github.com/air-verse/air@latestCreate .env File
Store your database connection string and server port in .env. godotenv will load these at startup.
Define the Database Model
Create models/user.go. Embedding gorm.Model gives you ID, CreatedAt, UpdatedAt, and DeletedAt for free. GORM uses struct tags to map fields to columns.
Create Request Handlers
Create handlers/user.go. Handlers receive a *gin.Context which gives access to request data, path params, and response helpers. GORM queries are fully type-safe.
Create the Main Entry Point
Create main.go. Connect to PostgreSQL, run AutoMigrate to create tables, register routes, and start the server.
Run the API
Use Air for development (auto-reloads on save) or run directly with go run.
airFound this guide helpful? Check out more tools and guides.
Browse All GuidesPrerequisites
- Go 1.22+ installed (go.dev/dl)
- PostgreSQL running locally or a connection string
- Air installed for hot-reload