命令行
提示
为方便用户可以快速构建项目和代码,本框架还提供了一个命令行工具 tg
。 除了 init
命令可以在任意目录执行外,sync
和 create
命令都需要在 think-go
的项目中执行,因为需要读取数据库的配置以及生产文件到对应目录中,任意位置执行会导致出错。
安装
通过下面的命令全局安装 tg
命令行工具
git clone https://github.com/think-go/tg.git && cd tg/cmd/tg && go install
git clone https://github.com/think-go/tg.git && cd tg/cmd/tg && go install
然后可以在全局调用 tg
命令查看帮助

创建项目
通过命令可以在你执行命令的当前目录创建项目工程
tg init demoApp
tg init demoApp
同步数据库表结构体
通过 tg sync 表名
的方式可以在 app/entity
目录下生成对应的结构体文件,不传表名会生成数据库中所有表对应的结构体文件
tg sync user
tg sync user
例如我这里生成一个user表的
package entity
import "time"
// User 有NULL值的注意加*防止转换报错
type User struct {
Id int `db:"id" json:"id"`
Name *string `db:"name" json:"name"`
Avatar *string `db:"avatar" json:"avatar"`
Phone *string `db:"phone" json:"phone"`
Email *string `db:"email" json:"email"`
Password *string `db:"password" json:"password"`
Code *int `db:"code" json:"code"`
CodeTime *time.Time `db:"code_time" json:"code_time"`
AppIds *string `db:"app_ids" json:"app_ids"`
CreateTime *time.Time `db:"create_time" json:"create_time"`
UpdateTime *time.Time `db:"update_time" json:"update_time"`
DeleteTime *time.Time `db:"delete_time" json:"delete_time"`
}
package entity
import "time"
// User 有NULL值的注意加*防止转换报错
type User struct {
Id int `db:"id" json:"id"`
Name *string `db:"name" json:"name"`
Avatar *string `db:"avatar" json:"avatar"`
Phone *string `db:"phone" json:"phone"`
Email *string `db:"email" json:"email"`
Password *string `db:"password" json:"password"`
Code *int `db:"code" json:"code"`
CodeTime *time.Time `db:"code_time" json:"code_time"`
AppIds *string `db:"app_ids" json:"app_ids"`
CreateTime *time.Time `db:"create_time" json:"create_time"`
UpdateTime *time.Time `db:"update_time" json:"update_time"`
DeleteTime *time.Time `db:"delete_time" json:"delete_time"`
}
创建一个业务模块
通过 tg create 模块名
的方式可以在 api
app/controller
app/service
app/dao
目录下生成一套增删改查的业务代码,您只需要在其基础上做对应的修改就可快速完成您的业务逻辑开发
tg create user
tg create user
例如我这里生成一个user的业务模块
api/user.go
package api
import "think-go/app/entity"
type CreateUserReq struct{}
type DeleteUserReq struct {
Id int `p:"id" v:"required"`
}
type EditUserReq struct {
Id int `p:"id" v:"required"`
}
type UserListReq struct {
Id int `p:"id"`
PageNum int `p:"pageNum"`
PageSize int `p:"pageSize"`
}
type UserListRes struct {
List []entity.User `json:"list"`
Total int `json:"total"`
}
package api
import "think-go/app/entity"
type CreateUserReq struct{}
type DeleteUserReq struct {
Id int `p:"id" v:"required"`
}
type EditUserReq struct {
Id int `p:"id" v:"required"`
}
type UserListReq struct {
Id int `p:"id"`
PageNum int `p:"pageNum"`
PageSize int `p:"pageSize"`
}
type UserListRes struct {
List []entity.User `json:"list"`
Total int `json:"total"`
}
app/controller/user.go
package controller
import (
"github.com/think-go/tg"
"think-go/api"
"think-go/app/service"
)
func CreateUser(ctx *tg.Context) {
var req api.CreateUserReq
ctx.BindStructValidate(&req)
err := service.UserService().CreateUser(req)
if err != nil {
ctx.Fail("创建失败")
return
}
ctx.Success("ok")
}
func DeleteUser(ctx *tg.Context) {
var req api.DeleteUserReq
ctx.BindStructValidate(&req)
err := service.UserService().DeleteUser(req.Id)
if err != nil {
ctx.Fail("删除失败")
return
}
ctx.Success("ok")
}
func EditUser(ctx *tg.Context) {
var req api.EditUserReq
ctx.BindStructValidate(&req)
err := service.UserService().EditUser(req)
if err != nil {
ctx.Fail("更新失败")
return
}
ctx.Success("ok")
}
func UserList(ctx *tg.Context) {
var req api.UserListReq
ctx.BindStructValidate(&req)
res, err := service.UserService().UserList(req)
if err != nil {
ctx.Fail("查询失败")
return
}
ctx.Success(res)
}
package controller
import (
"github.com/think-go/tg"
"think-go/api"
"think-go/app/service"
)
func CreateUser(ctx *tg.Context) {
var req api.CreateUserReq
ctx.BindStructValidate(&req)
err := service.UserService().CreateUser(req)
if err != nil {
ctx.Fail("创建失败")
return
}
ctx.Success("ok")
}
func DeleteUser(ctx *tg.Context) {
var req api.DeleteUserReq
ctx.BindStructValidate(&req)
err := service.UserService().DeleteUser(req.Id)
if err != nil {
ctx.Fail("删除失败")
return
}
ctx.Success("ok")
}
func EditUser(ctx *tg.Context) {
var req api.EditUserReq
ctx.BindStructValidate(&req)
err := service.UserService().EditUser(req)
if err != nil {
ctx.Fail("更新失败")
return
}
ctx.Success("ok")
}
func UserList(ctx *tg.Context) {
var req api.UserListReq
ctx.BindStructValidate(&req)
res, err := service.UserService().UserList(req)
if err != nil {
ctx.Fail("查询失败")
return
}
ctx.Success(res)
}
app/service/user.go
package service
import (
"think-go/api"
"think-go/app/dao"
)
type iUserService interface {
CreateUser(req api.CreateUserReq) (err error)
DeleteUser(id int) (err error)
EditUser(req api.EditUserReq) (err error)
UserList(req api.UserListReq) (res api.UserListRes, err error)
}
type UserServiceImpl struct{}
func (i UserServiceImpl) CreateUser(req api.CreateUserReq) (err error) {
err = dao.CreateUser(req)
return
}
func (i UserServiceImpl) DeleteUser(id int) (err error) {
err = dao.DeleteUser(id)
return
}
func (i UserServiceImpl) EditUser(req api.EditUserReq) (err error) {
err = dao.EditUser(req)
return
}
func (i UserServiceImpl) UserList(req api.UserListReq) (res api.UserListRes, err error) {
res, err = dao.UserList(req)
return
}
func UserService() iUserService {
return &UserServiceImpl{}
}
package service
import (
"think-go/api"
"think-go/app/dao"
)
type iUserService interface {
CreateUser(req api.CreateUserReq) (err error)
DeleteUser(id int) (err error)
EditUser(req api.EditUserReq) (err error)
UserList(req api.UserListReq) (res api.UserListRes, err error)
}
type UserServiceImpl struct{}
func (i UserServiceImpl) CreateUser(req api.CreateUserReq) (err error) {
err = dao.CreateUser(req)
return
}
func (i UserServiceImpl) DeleteUser(id int) (err error) {
err = dao.DeleteUser(id)
return
}
func (i UserServiceImpl) EditUser(req api.EditUserReq) (err error) {
err = dao.EditUser(req)
return
}
func (i UserServiceImpl) UserList(req api.UserListReq) (res api.UserListRes, err error) {
res, err = dao.UserList(req)
return
}
func UserService() iUserService {
return &UserServiceImpl{}
}
app/dao/user.go
package dao
import (
"github.com/think-go/tg"
"think-go/api"
"think-go/app/entity"
)
func CreateUser(req api.CreateUserReq) (err error) {
_, err = tg.Db("user").Insert(req)
return
}
func DeleteUser(id int) (err error) {
err = tg.Db("User").Where("id", "=", id).Delete()
return
}
func EditUser(req api.EditUserReq) (err error) {
err = tg.Db("User").Where("id", "=", req.Id).Update(req)
return
}
func UserList(req api.UserListReq) (res api.UserListRes, err error) {
m := tg.Db("User")
if req.PageNum == 0 {
req.PageNum = 1
}
if req.PageSize == 0 {
req.PageSize = 10
}
if req.Id != 0 {
m = m.Where("id", "=", req.Id)
}
var user []entity.User
count, err := m.Count()
err = m.Page(req.PageNum, req.PageSize).Select(&user)
res.List = user
res.Total = count
return
}
package dao
import (
"github.com/think-go/tg"
"think-go/api"
"think-go/app/entity"
)
func CreateUser(req api.CreateUserReq) (err error) {
_, err = tg.Db("user").Insert(req)
return
}
func DeleteUser(id int) (err error) {
err = tg.Db("User").Where("id", "=", id).Delete()
return
}
func EditUser(req api.EditUserReq) (err error) {
err = tg.Db("User").Where("id", "=", req.Id).Update(req)
return
}
func UserList(req api.UserListReq) (res api.UserListRes, err error) {
m := tg.Db("User")
if req.PageNum == 0 {
req.PageNum = 1
}
if req.PageSize == 0 {
req.PageSize = 10
}
if req.Id != 0 {
m = m.Where("id", "=", req.Id)
}
var user []entity.User
count, err := m.Count()
err = m.Page(req.PageNum, req.PageSize).Select(&user)
res.List = user
res.Total = count
return
}