Skip to content

goctl Commands

goctl is the code-generation CLI for go-zero. It generates complete service scaffolding from .api or .proto files, and produces DB models, Docker images, Kubernetes manifests, and more.

Terminal window
go install github.com/zeromicro/go-zero/tools/goctl@latest
goctl --version

Generate HTTP service scaffolding from a .api definition file.

Scaffold a new API project from scratch:

Terminal window
goctl api new <serviceName>
Terminal window
goctl api new order
cd order && go mod tidy && go run order.go -f etc/order-api.yaml

Generate Go code from an existing .api file:

Terminal window
goctl api go [flags]
FlagDefaultDescription
-apiPath to the .api file (required)
-dir.Output root directory
-stylegozeroFile naming style: gozero | go_zero | goZero
-home~/.goctlCustom template directory
-remoteRemote git template URL
-branchBranch for remote template
Terminal window
# Basic generation
goctl api go -api user.api -dir .
# With custom file naming style
goctl api go -api user.api -dir . -style go_zero
# Using custom templates
goctl api go -api user.api -dir . -home ./custom-templates

Check a .api file for syntax errors without generating code:

Terminal window
goctl api validate -api user.api

Format an .api file in place:

Terminal window
goctl api format -dir .

Generate Markdown documentation from a .api file:

Terminal window
goctl api doc -dir . -o ./docs

Generate gRPC service scaffolding from a .proto file.

Scaffold a new RPC project from scratch:

Terminal window
goctl rpc new <serviceName>

Generate from an existing .proto file:

Terminal window
goctl rpc protoc <proto-file> [flags]
FlagDefaultDescription
--go_outOutput directory for .pb.go files (required)
--go-grpc_outOutput directory for _grpc.pb.go files (required)
--zrpc_outOutput directory for zRPC service code (required)
-mfalseEnable multiple services in one proto file
--stylegozeroFile naming style
--home~/.goctlCustom template directory
--remoteRemote git template URL
--branchBranch for remote template
Terminal window
# Standard single-service generation
goctl rpc protoc user.proto \
--go_out=./pb \
--go-grpc_out=./pb \
--zrpc_out=.
# Multiple services in one file
goctl rpc protoc multi.proto \
--go_out=./pb \
--go-grpc_out=./pb \
--zrpc_out=. \
-m

Generate type-safe, zero-reflection data access code.

Generate from a SQL DDL file:

Terminal window
goctl model mysql ddl [flags]
FlagDefaultDescription
-srcPath to the .sql DDL file (required)
-dirOutput directory (required)
-cachefalseWrap generated code with Redis cache layer
-ideafalseSuppress progress output (for IDE plugins)
-stylegozeroFile naming style
-home~/.goctlCustom template directory
Terminal window
goctl model mysql ddl -src schema.sql -dir ./internal/model
goctl model mysql ddl -src schema.sql -dir ./internal/model -cache

Generate from a live MySQL connection:

Terminal window
goctl model mysql datasource [flags]
FlagDefaultDescription
-urlMySQL DSN (required)
-tableComma-separated table names, "*" for all
-dirOutput directory (required)
-cachefalseAdd Redis cache layer
-stylegozeroFile naming style
Terminal window
goctl model mysql datasource \
-url "root:password@tcp(127.0.0.1:3306)/mydb" \
-table "user,order,product" \
-dir ./internal/model \
-cache

Generate from a live PostgreSQL connection:

Terminal window
goctl model pg datasource [flags]
FlagDefaultDescription
-urlPostgreSQL DSN (required)
-tableTable name(s)
-schemapublicPostgreSQL schema
-dirOutput directory (required)
-cachefalseAdd Redis cache layer
-stylegozeroFile naming style
Terminal window
goctl model pg datasource \
-url "postgres://root:password@localhost:5432/mydb?sslmode=disable" \
-table "users" \
-dir ./internal/model

Generate MongoDB model code:

Terminal window
goctl model mongo [flags]
FlagDefaultDescription
-typeGo type name for the collection document
-dirOutput directory
-cachefalseAdd Redis cache layer
-easyfalseGenerate a simpler model interface
-stylegozeroFile naming style
Terminal window
goctl model mongo -type Article -dir ./internal/model -cache

Generate an optimized multi-stage Dockerfile:

Terminal window
goctl docker [flags]
FlagDefaultDescription
-goPath to main.go (required)
-port8888Container exposed port
-version1.22-alpineGo base image version
-home~/.goctlCustom template directory
-namespaceKubernetes namespace (for generated labels)
Terminal window
goctl docker -go main.go
goctl docker -go main.go -port 8080 -version 1.22-alpine

Generate Kubernetes Deployment + Service + HPA manifests:

Terminal window
goctl kube deploy [flags]
FlagDefaultDescription
-nameService name (required)
-namespaceKubernetes namespace (required)
-imageContainer image (required)
-portService port (required)
-oOutput YAML file
-minreplicas3HPA min replicas
-maxreplicas10HPA max replicas
-requestCpu500mCPU request
-requestMem512MiMemory request
-limitCpu1000mCPU limit
-limitMem1024MiMemory limit
Terminal window
goctl kube deploy \
-name order-api \
-namespace production \
-image myregistry/order-api:v1.2.0 \
-port 8888 \
-o k8s/order-api.yaml

Manage goctl’s code generation templates.

SubcommandDescription
goctl template initCopy default templates to ~/.goctl/
goctl template cleanRemove cached templates
goctl template updateForce-update templates to match current goctl version
goctl template revertRestore a single template to default
Terminal window
goctl template init
ls ~/.goctl/api/ # api templates
ls ~/.goctl/rpc/ # rpc templates
ls ~/.goctl/model/ # model templates

Check and auto-install required tools.

Terminal window
goctl env check [flags]
FlagDescription
--installInstall missing tools automatically
--verboseShow detailed output
Terminal window
goctl env check --install --verbose

Sample output:

goctl version: 1.7.x
go: 1.22.0
protoc: 25.1
protoc-gen-go: 1.33.0
protoc-gen-go-grpc: 1.3.0
goctl-intellij: OK
goctl-vscode: OK

Upgrade goctl to the latest release:

Terminal window
goctl upgrade

Style valueExample output
gozerogetuserhandler.go
go_zeroget_user_handler.go
goZerogetUserHandler.go