gitmyhub

go-gin-example

Go ★ 7.2k updated 3mo ago

An example of gin

A complete Go backend example project that shows how to wire together a REST API with authentication, MySQL, Redis caching, and file uploads using Gin, structured as a blog system you can learn from or use as a starter template.

GoGinGORMMySQLRedisJWTSwaggerDockersetup: moderatecomplexity 3/5

go-gin-example is a working demonstration of how to build a backend web API in the Go programming language. The project models a blog system: it handles articles and tags, user authentication, file uploads, and extras like QR code generation and Excel import/export. It is intended as a reference showing how common building blocks fit together in a Go project, not as a finished product for direct deployment.

The main technologies are Gin (a web framework for Go), GORM (a library that handles database interactions with MySQL), Redis (for caching frequently read data), JWT tokens (for authentication), and Swagger (for generating API documentation automatically). Each covers a distinct job: Gin manages HTTP routing, GORM reads and writes to MySQL, Redis reduces database load for popular queries, and JWT lets the server verify that a request comes from a logged-in user.

The project is structured in layers. HTTP requests first pass through middleware that checks authentication, then reach route handlers that validate parameters and format responses, then delegate work to a service layer that holds business logic, and finally the service layer talks to models that interact with MySQL or the Redis cache.

Beyond article and tag management, the codebase includes utilities for image uploads, generating QR codes embedded into poster images, reading and writing Excel files, and structured logging. These shared helpers live in a pkg/ directory so the rest of the code can import them cleanly.

A Dockerfile and Makefile are included for build automation. The project uses Go modules for dependency management. The README notes it was written as a companion to a series of Chinese-language blog posts on building web services with Go, so some linked documentation is in Chinese.

Where it fits