Explore the Complete Gin-Based Backend Project Structure and Core Code
This guide walks through the automatically generated Gin framework project, detailing the directory layout—including API, model, service, config, database, migrations, public assets, routers, runtime, and utils—while providing sample commands and code snippets that illustrate how to initialize, define interfaces, and perform CRUD operations with GORM.
Run the following commands to generate the project scaffold from the open‑source gingen tool:
mkdir testServer
cd testServer
./gingen init --mod testServerProject repository: github.com/Benny66/gin
app
The project’s main codebase is organized into several layers:
API layer – request entry points, parameter validation, and response handling.
Model layer – data‑access layer with common database operations and aggregation methods.
Service layer – core business‑logic processing.
API layer
The API layer, together with the schema module, receives request parameters, performs validation and sanitization, and returns results.
type UserInterface interface {
Login(context *gin.Context)
Refresh(context *gin.Context)
Logout(context *gin.Context)
UpdatePassword(context *gin.Context)
}
var UserApi UserInterface = &userApi{}
type userApi struct{}model (Model layer)
The model defines the database tables and fields.
ModelTime handles automatic conversion of time formats for storage and queries.
Models can specify table names and column mappings.
The DAO, built on GORM, provides CRUD operations via interfaces.
Create and Update operations run within transactions, controlled by the service layer.
Common helper methods include pagination, FindAll, WhereQuery, Joins, Preloads, etc.
func (dao *userDao) Create(tx *gorm.DB, data *model.User) (rowsAffected int64, err error) {
db := tx.Create(data)
if err = db.Error; db.Error != nil {
return
}
rowsAffected = db.RowsAffected
return
}
func (dao *userDao) WhereQuery(query interface{}, args ...interface{}) *userDao {
return &userDao{dao.gm.Where(query, args...)}
}
func (dao *userDao) Joins(query string, args ...interface{}) *userDao {
return &userDao{dao.gm.Joins(query, args)}
}service (Logic layer)
The service layer contains the main business logic where developers implement features and fix bugs.
API module maps routes to handler functions (e.g., user.go for login APIs).
Define module declares request and response structs (e.g., type UserLoginApiReq struct ).
Service module validates parameters, processes business logic, and calls the data layer.
router.go centralizes route definitions.
config (System configuration)
Configuration files (config.go) include service settings, database connections, logging, WebSocket options, etc.
db (Database)
Database package currently uses MySQL and Redis for data storage.
migrations (Data migration)
Migrations contain scripts for initializing or upgrading the database; an install.lock file must exist in the project root to enable migration execution.
public (Public assets)
Public module holds static assets such as images, HTML, CSS, and JavaScript files.
routers (Routing)
When the web service starts, Gin’s router module is initialized to handle incoming requests.
runtime
Runtime module stores logs, cache files, and other runtime data.
utils
Utility package provides custom libraries and third‑party wrappers required by the framework.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
