9.1 CLI工具概述
9.1.1 GoFrame CLI安装
go install github.com/gogf/gf/cmd/gf/v2@latest
gf version
9.1.2 基本命令
gf init my-project
gf gen model -t=mysql -d=mydb -o=./model
gf run main.go
9.2 项目脚手架
9.2.1 项目初始化
gf init my-web-project --repo=github.com/myuser/my-web-project
9.2.2 项目结构最佳实践
my-web-project/
│
├── api/ # API接口定义
├── internal/ # 内部实现
│ ├── controller/ # 控制器
│ ├── service/ # 服务层
│ ├── model/ # 数据模型
│ └── dao/ # 数据访问
│
├── config/ # 配置文件
├── deploy/ # 部署脚本
├── docs/ # 文档
├── scripts/ # 工具脚本
├── test/ # 测试用例
├── go.mod
└── main.go
9.2.3 多环境配置
server:
address: ":8000"
server:
address: ":8001"
debug: true
server:
address: ":80"
debug: false
9.3 代码生成器
9.3.1 模型(Model)生成
gf gen model -t=mysql -d=mydb -g=users,products
type User struct {
g.Model
Username string `json:"username" orm:"username"`
Email string `json:"email" orm:"email"`
Status int `json:"status" orm:"status"`
}
func (u *User) Validators(