Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate model automatically by xorm in go language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how xorm automatically generates model in go language". In daily operation, I believe that many people have doubts about how to automatically generate model in xorm in go language. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to automatically generate model in xorm language in go language". Next, please follow the editor to study!

Xorm automatically generates model files

Enter the project root directory 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

If there is no go-sql-driver/mysql, install it first.

Go get-u github.com/go-sql-driver/mysql

Install xorm, under the cmd command line

Go get github.com/go-xorm/xorm

Then install xorm's cmd command tool

Go get github.com/go-xorm/cmd/xorm

Install cmd error handling

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 () .LogLevel .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 () .LogLevel% jump out of the project directory for execution Then jump back to 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% to create a templates/goxorm folder under the project directory.

Create config and struct.go.tpl files under this file.

The content of the template can be modified according to your own needs

The config content is as follows

Lang=gogenJson=1prefix=

The struct.go.tpl content is as follows

Package {{$models}} {{$ilen: = len .imports}} {{if gt $ilen 0}} import ({{range .imports} "{{.}" {end}) {{end} {{range .Tables}} type {{Mapper .Name}} struct {{$table: =.} {{Mapper $col: = $table.GetColumn.} {{Mapper $col.Name}} {Type $col}} {Tag $table $col} }} {{end} {{end}}

Finally execute the order.

The program generates the models folder in the current directory and the go file in the models folder

Xorm reverse mysql root:root@/fox?charset=utf8 templates/goxorm execution error 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

Solution

Vim / .zshrc

Add to ~ / .zshrc:

Setopt no_nomatch

Check it out.

Luwei@luweideMacBook-Pro-2 go-simple-task% cat ~ / .zshrc source ~ / .bash_profileexport GOPROXY= https://goproxy.io,directsetopt no_nomatch

Final execution

Source ~ / .zshrc execute luwei@luweideMacBook-Pro-2 go-simple-task% xorm reverse mysql root:123456@/bubble?charset=utf8 templates/goxormluwei@luweideMacBook-Pro-2 go-simple-task% again

The rest is to assign the contents of models to the corresponding model of model, and the study of "how to automatically generate model in xorm in go language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report