Model Generation

First, after downloading the demo project, we will use the user's model to demonstrate the code generation.


Model is a bridge for services to access the persistent data layer. The persistent data of the business often exists in databases such as mysql and mongo. We all know that the operation of a database is nothing more than CURD. And these tasks will also take up part of the time for development. I once wrote 40 model files when writing a business. According to the complexity of different business requirements, on average, each model file is almost required. 10 minutes, for 40 files, 400 minutes of working time, almost a day's workload, and the goctl tool can complete the 400 minutes of work in 10 seconds.


Enter the demo project book, find the user.sql file under user/model, and execute the table creation in your own database.

Code generation (with cache)#

The way one(ddl)#

Enter the service/user/model directory and execute the command

$ cd service/user/model
$ goctl model mysql ddl -src user.sql -dir . -c

The way two(datasource)#

$ goctl model mysql datasource -url="$datasource" -table="user" -c -dir .

$datasource is the database connection address

The way three(intellij plugin)#

In Goland, right-click user.sql, enter and click New->Go Zero->Model Code to generate it, or open the user.sql file, Enter the editing area, use the shortcut key Command+N (for macOS) or alt+insert (for windows), select Model Code.

model generation


The intellij plug-in generation needs to install the goctl plug-in, see intellij plugin for details

Verify the generated model file#

view tree

$ tree
├── user.sql
├── usermodel.go
├── usermodel_gen.go
└── vars.go


For persisting data, if more flexible database capabilities are required, including transactional capabilities, see Mysql

For distributed transaction capabilities, see

Last updated on