a lib for golang , generate mysql table schema to golang struct
Automatic generation of golang struct from mysql table structure
github address
https://github.com/gohouse/converter
install
- Download the executable directly: Download address
- golang source package: go get github.com/gohouse/converter
Sample table structure
CREATE TABLE `prefix_user` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `Email` varchar(32) NOT NULL DEFAULT '' COMMENT 'mailbox', `Password` varchar(32) NOT NULL DEFAULT '' COMMENT 'Password', `CreatedAt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='User table'
Command line usage
- Download the executable file of the corresponding platform, Download address
-
Command line execution
# File name: table2struct-[$platform].[$version].[$suffix] ./table2struct-linux.v0.0.3.bin -file model.go -dsn xxx -table user
- Parameter description
-dsn string database dsn configuration -Enablejsontag bool whether to add the tag of json -File string save path -Packagename string package name -Prefix string table prefix -Table name corresponding to realNameMethod string structure -Table string table to migrate -Tag key string key of tag
Simple usage of golang code
package 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) }
Detailed usage example of golang code
package main import ( "fmt" "github.com/gohouse/converter" ) func main() { // Initialization t2t := converter.NewTable2Struct() // Personalized configuration t2t.Config(&converter.T2tConfig{ // If the initial of the field is capitalized, tag will not be added, false will be added by default, and true will not be added RmTagIfUcFirsted: false, // Whether the field name of tag is converted to lowercase? If it has uppercase letters, the default value is false TagToLower: false, // If you want to convert other letters to lowercase while the initial of the field is uppercase, false will not be converted by default UcFirstOnly: false, ////Put each struct into a separate file, false by default, and put it into the same file (not provided yet) //SeperatFile: false, }) // Start migration transformation err := t2t. // Specify a table. If not specified, all tables will be migrated by default Table("user"). // Table prefix Prefix("prefix_"). // Add json tag or not EnableJsonTag(true). // Package name of the generated struct (if it is empty by default, it will be named: package model) PackageName("model"). // The key value of the tag field, which is orm by default TagKey("orm"). // Add structure method to get table name RealNameMethod("TableName"). // Generated structure save path SavePath("/Users/fizz/go/src/github.com/gohouse/gupiao/model/model.go"). // Database dsn, which can be replaced by t2t.DB(). The parameter is the * sql.DB object Dsn("root:root@tcp(localhost:3306)/test?charset=utf8"). // implement Run() fmt.Println(err) }
result
package model import "time" type User struct { Id int `json:"Id" orm:"Id"` Email string `json:"Email" orm:"Email"` // mailbox Password string `json:"Password" orm:"Password"` // Password CreatedAt string `json:"CreatedAt" orm:"CreatedAt"` } func (*User) TableName() string { return "user" }