你懂的,手工翻译表定义到go结构体是很枯燥的。
so,用xorm搞定。
go get github.com/go-xorm/cmd/xorm
1
安装以下依赖,用到哪个装哪个。
github.com/go-xorm/xorm
驱动
Mysql: github.com/go-sql-driver/mysql
MyMysql: github.com/ziutek/mymysql/godrv
Postgres: github.com/lib/pq
SQLite: github.com/mattn/go-sqlite3
MSSQL: github.com/denisenkom/go-mssqldb
逆向生成
Reverse 命令可以转换数据库到所有支持的语言的数据结构,安装以后可以用 xorm help reverse查看帮助。
例子:
cd $GOPATH/src/github.com/go-xorm/cmd/xorm
sqlite: xorm reverse sqite3 test.db templates/goxorm
mysql: xorm reverse mysql root:root@/xorm_test?charset=utf8 templates/goxorm
mymysql: xorm reverse mymysql xorm_test2/root/ templates/goxorm
postgres: xorm reverse postgres "dbname=xorm_test sslmode=disable" templates/goxorm
mssql: xorm reverse mssql "server=test;user id=testid;password=testpwd;database=testdb" templates/goxorm
会在./model目录下生成go的文件
坑
一定要在$GOPATH/src/github.com/go-xorm/cmd/xorm目录下运行,因为在这个目录下有templets,在解析数据库结构的时候有用。如果在别的目录下运行,会导致命令不报错,但是无法正常生成对应的结构文件。有空可以给github.com/go-xorm/cmd/xorm提个bug,加上错误提示。
---------------------
作者:zgcbj
来源:CSDN
原文:https://blog.csdn.net/lizao2/article/details/80272213
版权声明:本文为博主原创文章,转载请附上博文链接!