这篇文章主要介绍“Go语言中xORM如何自动生成model”,在日常操作中,相信很多人在go语言中xorm如何自动生成model问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”go语言中xorm如何自动生成mo
这篇文章主要介绍“Go语言中xORM如何自动生成model”,在日常操作中,相信很多人在go语言中xorm如何自动生成model问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”go语言中xorm如何自动生成model”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
xorm自动生成model文件
luwei@luweideMacBook-Pro-2 go-simple-task % pwd/Users/myself/golang/common_project/src/go-simple-taskluwei@luweideMacBook-Pro-2 go-simple-task % lsDockerfile go-simple-task go.mod go.sum logic logs main.go model models overall serv templates toolluwei@luweideMacBook-Pro-2 go-simple-task %
go get -u GitHub.com/go-sql-driver/Mysql
安装xorm,在cmd命令行下
go get github.com/go-xorm/xorm
再安装xorm的cmd命令工具
go get github.com/go-xorm/cmd/xorm
安装 cmd 报错处理
luwei@luweideMacBook-Pro-2 go-simple-task % go get github.com/go-xorm/cmd/xorm# github.com/go-xorm/cmd/xorm../../pkg/mod/github.com/go-xorm/cmd/xorm@v0.0.0-20190426080617-f87981e709a1/dump.go:45:26: cannot use "github.com/go-xorm/core".LOG_UNKNOWN (type "github.com/go-xorm/core".LogLevel) as type "xorm.io/core".LogLevel in argument to engine.Logger().SetLevel../../pkg/mod/github.com/go-xorm/cmd/xorm@v0.0.0-20190426080617-f87981e709a1/reverse.go:246:34: cannot use tables (type []*"xorm.io/core".Table) as type []*"github.com/go-xorm/core".Table in argument to langTmpl.GenImports../../pkg/mod/github.com/go-xorm/cmd/xorm@v0.0.0-20190426080617-f87981e709a1/reverse.go:254:18: cannot use table (type *"xorm.io/core".Table) as type *"github.com/go-xorm/core".Table in append../../pkg/mod/github.com/go-xorm/cmd/xorm@v0.0.0-20190426080617-f87981e709a1/reverse.go:291:26: cannot use table (type *"xorm.io/core".Table) as type *"github.com/go-xorm/core".Table in slice literal../../pkg/mod/github.com/go-xorm/cmd/xorm@v0.0.0-20190426080617-f87981e709a1/shell.go:60:26: cannot use "github.com/go-xorm/core".LOG_UNKNOWN (type "github.com/go-xorm/core".LogLevel) as type "xorm.io/core".LogLevel in argument to engine.Logger().SetLevel../../pkg/mod/github.com/go-xorm/cmd/xorm@v0.0.0-20190426080617-f87981e709a1/source.go:45:26: cannot use "github.com/go-xorm/core".LOG_UNKNOWN (type "github.com/go-xorm/core".LogLevel) as type "xorm.io/core".LogLevel in argument to engine.Logger().SetLevelluwei@luweideMacBook-Pro-2 go-simple-task %
luwei@luweideMacBook-Pro-2 go-simple-task % cd ../luwei@luweideMacBook-Pro-2 src % go get github.com/go-xorm/cmd/xormluwei@luweideMacBook-Pro-2 src %
这个文件下建立config和struct.go.tpl文件.
模板内容可以根据你自己的需要修改
config内容如下
lang=gogenJSON=1prefix=
struct.go.tpl内容如下
package {{.Models}}{{$ilen := len .Imports}}{{if gt $ilen 0}}import ( {{range .Imports}}"{{.}}"{{end}}){{end}}{{range .Tables}}type {{Mapper .Name}} struct {{{$table := .}}{{range .ColumnsSeq}}{{$col := $table.GetColumn .}} {{Mapper $col.Name}} {{Type $col}} {{Tag $table $col}}{{end}}}{{end}}
最后执行命令
程序会在当前目录下生成models文件夹,并在models文件夹中生成go文件
xorm reverse mysql root:root@/fox?charset=utf8 templates/goxorm
luwei@luweideMacBook-Pro-2 go-simple-task % xorm reverse mysql root:123456@/bubble?charset=utf8 templates/goxormzsh: no matches found: root:123456@/bubble?charset=utf8
解决方案
vim ~/.zshrc
在~/.zshrc中加入:
setopt no_nomatch
检查一下
luwei@luweideMacBook-Pro-2 go-simple-task % cat ~/.zshrc source ~/.bash_profileexport GOPROXY=https://goproxy.io,directsetopt no_nomatch
最后执行
source ~/.zshrc
luwei@luweideMacBook-Pro-2 go-simple-task % xorm reverse mysql root:123456@/bubble?charset=utf8 templates/goxormluwei@luweideMacBook-Pro-2 go-simple-task %
到此,关于“go语言中xorm如何自动生成model”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
--结束END--
本文标题: go语言中xorm如何自动生成model
本文链接: https://lsjlt.com/news/304550.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0