Gen Tool is a single binary without dependencies can be used to generate structs from database
Install
go install gorm.io/gen/tools/gentool@latest |
Version and Go compatibility
The tool module (gorm.io/gen/tools/gentool, currently v0.0.3) and the
library module (gorm.io/gen, currently v0.3.29) version independently —
check both when reporting issues.
The tool module and its current dependency set (e.g. golang.org/x/tools
v0.40.0) declare go 1.24.0, so building gentool requires Go 1.24 or newer;
the generated code and the gorm.io/gen library itself only require
Go 1.18+ (-useAny likewise only needs Go 1.18 in the target module). If
you write your own generator program importing gorm.io/gen v0.3.29 under a
recent toolchain and hit build errors inside golang.org/x/tools, bumpgolang.org/x/tools in your module to a version compatible with your toolchain
(gen’s minimum pin v0.17.0 predates current compilers).
Usage
gentool -h |
c
Path to a YAML configuration file. When -c is set, the configuration file
completely replaces command-line flags — every other flag on the same
invocation is ignored, even if set. Use either flags or a config file, not both.
db
Specify driver dialector, default value “mysql”, refer: https://gorm.io/docs/connecting_to_the_database.html
Supported: mysql, postgres, sqlite, sqlserver, clickhouse. (The tool’s
own error message for an unknown -db omits clickhouse, but it is supported.)
dsn
DSN that used to connect database, refer: https://gorm.io/docs/connecting_to_the_database.html
fieldNullable
Generate with pointer when field is nullable
fieldWithIndexTag
Generate field with gorm index tag
fieldWithTypeTag
Generate field with gorm column type tag
modelPkgName
Generated model code’s package name.
outFile
Generated query code file name, default: gen.go. It names the entry file
(Use/SetDefault/Q) only; every model always gets its own <model>.gen.go
regardless.
outPath
Specify a directory for output (default “./dao/query”)
tables
Specify tables want to be generated from, default all tables. Names are
comma-separated, whitespace-trimmed, empty entries dropped.
eg :
--tables="orders" # generate from `orders`
--tables="orders,users" # generate from `orders` and `users`
--tables="" # generate from all tables
Generate some tables code.
withUnitTest
Generate unit test, default value false, options: false / true
fieldSignable
Use signable datatype as field type, default value false, options: false / true
Example
gentool -dsn "user:pwd@tcp(localhost:3306)/database?charset=utf8mb4&parseTime=True&loc=Local" -tables "orders,doctor" |
version: "0.1" |
Note that tables must be a list — a scalar string fails YAML decoding withcannot unmarshal !!str ... into []string.