database table to golang struct (table to struct) converter with cli and go lib support
database table to golang struct (table to struct) converter with cli and go lib support
mysql表结构自动生成golang struct
https://github.com/gohouse/converter
go get github.com/gohouse/converterCREATE TABLE `prefix_user` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Email` varchar(32) NOT NULL DEFAULT '' COMMENT '邮箱',
`Password` varchar(32) NOT NULL DEFAULT '' COMMENT '密码',
`CreatedAt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表'下载对应平台的可执行文件, 下载地址
命令行执行
# 文件名: table2struct-[$platform].[$version].[$suffix]
./table2struct-linux.v0.0.3.bin -file model.go -dsn xxx -table user参数说明
-dsn string 数据库dsn配置
-enableJsonTag bool 是否添加json的tag
-file string 保存路径
-packageName string 包名
-prefix string 表前缀
-realNameMethod string 结构体对应的表名
-table string 要迁移的表
-tagKey string tag的keypackage main
import (
"fmt"
"github.com/gohouse/converter"
)
func main() {
err := converter.NewTable2Struct().
SavePath("/home/go/project/model/model.go").
Dsn("root:root@tcp(localhost:3306)/test?charset=utf8").
Run()
fmt.Println(err)
}…result
package model
import "time"
type User struct {
Id int `json:"Id" orm:"Id"`
Email string `json:"Email" orm:"Email"` // 邮箱
Password string `json:"Password" orm:"Password"` // 密码
CreatedAt string `json:"CreatedAt" orm:"CreatedAt"`
}
func (*User) TableName() string {
return "user"
}No open issues yet, or sync has not completed.