gocli is a command-line toolset written in Go, designed to boost development efficiency. It currently includes features for code generation and quick project scaffolding.
go install github.com/morehao/gocli@latestgenerate is a powerful code generation tool based on template files and database schema. The project structure and style are modeled after goark.
- 🚀 Fast Development: Quickly generate a complete CRUD module based on MySQL/PostgreSQL table structure
- 📦 Multi-Layer Generation: Supports model, dao, service, controller, dto, router, and more
- 🎯 Three Generation Modes: module (full module), model (data layer only), api (single API endpoint)
- 🔧 Highly Customizable: Configure layer names, parent directories, and file name prefixes
- ✨ Auto Formatting: Automatically formats generated code using
gofmt - 📖 Database-Driven: Reads MySQL/PostgreSQL table structure to generate accurate model definitions
Generates a complete CRUD module including all layers:
- model: Database model
- dao: Data Access Object
- object: Business object
- controller: HTTP request handler
- service: Business logic layer
- dto: Request/Response objects
- router: Route registration
- code: Error code definitions
Use Case: Creating a new feature module from scratch
gocli generate module -a demoappGenerates only the data layer code:
- model: Database model with GORM tags
- dao: Data access methods
- object: Business object for data transformation
Use Case: Adding a new database table without full CRUD operations
gocli generate model -a demoappAdds a new API endpoint to an existing module:
- controller: New controller method
- service: New service method
- dto: Request/Response structs
- router: Route registration
Use Case: Adding a new endpoint to an existing feature
gocli generate api -a demoappRoute style: Endpoint paths follow the RESTful convention /{kebab-case plural resource}/{action}, consistent with the module mode. For example, deleting a login log (target_filename: user_login_log.go, function_name: Delete, http_method: POST) generates:
POST /v1/demoapp/user-login-logs/delete
- Execute in project root: Run the command in the project root directory (e.g.,
go-gin-web) - Specify app name: Use the
--appparameter to specify the application name (e.g.,demoapp) - Configuration file required: Ensure
apps/{appName}/config/code_gen.yamlexists
Example configuration file:
database_dsn: mysql://root:123456@tcp(127.0.0.1:3306)/demo?charset=utf8mb4&parseTime=True&loc=Local
service_name: mysql
module:
package_name: user
description: User login records
table_name: user_login_log
table_prefix: "" # Optional: Table name prefix, will be removed when generating struct name (e.g., "iam_")
model:
package_name: user
description: User
table_name: user
table_prefix: "" # Optional: Table name prefix
api:
package_name: user
target_filename: user_login_log.go
function_name: Delete
http_method: POST
description: Delete login record
api_doc_tag: User login recordsDatabase Connection Format:
| Database Type | DSN Format |
|---|---|
| MySQL | mysql://user:password@tcp(host:port)/dbname?params |
| PostgreSQL | postgresql://user:password@host:port/dbname?params |
Examples:
# MySQL
database_dsn: mysql://root:123456@tcp(127.0.0.1:3306)/demo?charset=utf8mb4&parseTime=True&loc=Local
# PostgreSQL
database_dsn: postgresql://postgres:password@localhost:5432/demo?sslmode=disable| Field | Description | Example | Required |
|---|---|---|---|
database_dsn |
Database connection string, format: schema://dsn | mysql://root:123456@tcp(127.0.0.1:3306)/demo?charset=utf8mb4&parseTime=True&loc=Local |
✅ Yes |
service_name |
Layer name prefix for model/dao directories and DB connection name | mysql |
✅ Yes |
| Field | Description | Example | Required |
|---|---|---|---|
package_name |
Package name for the module | user |
✅ Yes |
description |
Module description (for comments) | User login records |
✅ Yes |
table_name |
Database table name | user_login_log |
✅ Yes |
table_prefix |
Table name prefix, removed when generating struct name | iam_ |
❌ Optional |
| Field | Description | Example | Required |
|---|---|---|---|
package_name |
Package name for the model | user |
✅ Yes |
description |
Model description | User |
✅ Yes |
table_name |
Database table name | user |
✅ Yes |
table_prefix |
Table name prefix, removed when generating struct name | iam_ |
❌ Optional |
| Field | Description | Example | Required |
|---|---|---|---|
package_name |
Package name for the API | user |
✅ Yes |
target_filename |
Target file name for generated code | user_login_log.go |
✅ Yes |
function_name |
Function/method name | Delete |
✅ Yes |
http_method |
HTTP method | POST, GET, PUT, DELETE |
✅ Yes |
description |
API description | Delete login record |
✅ Yes |
api_doc_tag |
Swagger/API doc tag | User login records |
✅ Yes |
# Run commands in the project root directory (e.g., go-gin-web)
# Generate a complete module (model + dao + service + controller + dto + router + code)
gocli generate module -a demoapp
# Generate only data layer (model + dao + object)
gocli generate model -a demoapp
# Generate a single API endpoint (controller + service + dto + router)
gocli generate api -a demoappParameters:
-a, --app: Application name, e.g.,demoapp(required)
Quick Tips:
- 💡 Use
modulewhen starting a new feature from scratch - 💡 Use
modelwhen you only need database models - 💡 Use
apito add new endpoints to existing modules - 💡 Check the goark
Makefilefor practical examples
When you run gocli generate module -a demoapp, the tool generates:
apps/demoapp/
├── model/ # Database models
│ └── user.go
├── demoappdao/ # Data access layer (named as {appName}dao)
│ └── user.go
├── object/ # Business objects
│ └── objuser/
│ └── user.go
├── internal/
│ ├── controller/ # HTTP handlers
│ │ └── ctruser/
│ │ └── user.go
│ ├── service/ # Business logic
│ │ └── svcuser/
│ │ └── user.go
│ ├── dto/ # Request/Response DTOs
│ │ └── dtouser/
│ │ ├── request.go
│ │ └── response.go
│ └── router/ # Route registration
│ ├── router.go
│ └── user.go
pkg/code/ # Shared error codes
└── user.go
Note: The dao layer is generated as a single-level directory named {appName}dao (e.g., demoappdao), using gormdao.Dao for common CRUD operations.
create is a scaffolding tool that builds a backend monorepo project from the built-in template, or adds a new app to an existing monorepo. The template is embedded in the binary via go:embed, so it works offline after go install.
- Generates a complete backend monorepo from the built-in template:
go.work(workspace) +apps/demoapp(sample app) +pkg(shared library) - Automatically rewrites module paths and imports (
github.com/example→ your module path) - Runs
go work syncby default (disable with--no-tidy) - Optionally runs
git initwith--git
gocli create project my-backend -m github.com/acme/backend
cd my-backendGenerated structure:
my-backend/
├── go.work # workspace: apps/demoapp + pkg
├── apps/
│ └── demoapp/ # sample app (gin + gorm)
│ ├── config/code_gen.yaml
│ ├── go.mod
│ └── internal/ ... # controller/service/dto/router layers
└── pkg/ # shared library (code, dbclient)
└── go.mod
Flags:
-m, --module: root module path, e.g.github.com/acme/backend(required)- positional
[dir]: destination directory (defaults to the last segment of the module path) --git: initialize a git repository--no-tidy: skipgo work sync
- Run from the monorepo root; creates a new app under
backend/apps/<name>from the built-in sample app - Automatically rewrites module paths, imports, package names, and app names in configs/comments
- Automatically registers the app into
go.work(use ./backend/apps/<name>) - Runs
go work syncat the workspace root by default to sync dependencies (disable with--no-tidy)
cd /path/to/my-backend
gocli create app -n userappFlags:
-n, --name: new app name (required; lowercase letters and digits only)-m, --module: override the app module path--no-tidy: skipgo work sync
Module path inference order for the new app:
--moduleif provided- Parent path of the
pkg/go.modmodule (e.g.github.com/acme/backend/pkg→github.com/acme/backend) - Root
go.modmodule path, or the parent path of anyapps/*/go.modmodule
The new app works seamlessly with generate:
gocli generate module -a userapp # generate code from DB schemacutter is a CLI tool for quickly creating a new Go project based on an existing template project, or cloning an app within the same project.
- Must be executed from the template project root, which may contain either
go.modorgo.work. - Filters copied files using
.gitignore. - Replaces import paths automatically.
- Rewrites the root module name only when the copied destination root contains
go.mod. - Deletes the
.gitdirectory from the new project.
⚠️ Note: Run the command from the project root wherego.modorgo.workis located.
- Clone an existing app to a new app within the same project.
- Must be executed from the project root directory.
- Supports both single-module projects and workspace projects.
- Automatically replaces package names and import paths.
- Replaces app names in configuration files (
.yaml,.yml). - Follows
.gitignorerules.
Module path resolution for cutter app uses this priority:
apps/<source>/go.mod- root
go.mod - inferred module from
go.work use
Additional rules:
- If the source app already has its own
go.mod, that app module path is used first. - If the module cannot be resolved uniquely,
cutter appreturns a clear error instead of guessing.
cd /appTemplatePath
gocli cutter -d /yourAppPathParameters:
-d, --destination: Destination path for the new project, e.g.,/user/myApp(required).
# Run in project root directory (e.g., go-gin-web)
cd /path/to/go-gin-web
# Clone demoapp to newapp
gocli cutter app -n newapp
# Or specify source app
gocli cutter app -s demoapp -n myappParameters:
-s, --source: Source app name to clone from (default:demo).-n, --name: New app name (required).
Example:
# Clone apps/demoapp to apps/userapp
gocli cutter app -n userapp
# Clone apps/demoapp to apps/adminapp
gocli cutter app -s demoapp -n adminappThis command will:
- Copy the entire app directory structure
- Resolve the source app module path from
apps/<source>/go.mod, rootgo.mod, orgo.work use - Prefer the app module path when the source app has its own
go.mod - Replace import paths, for example:
- app module:
example.com/apps/demoapp/...→example.com/apps/newapp/... - root module:
example.com/root/apps/demoapp/...→example.com/root/apps/newapp/...
- app module:
- Replace app names in configuration files
- Maintain proper Go code formatting