golang生成dao代码

Python038

golang生成dao代码,第1张

golang服务端项⽬代码⾃动⽣成

公司为了提⾼开发效率,让我搞⼀个代码⾃动⽣成的⼯具,最好是根据数据库可以⽣成全套的(从router到dao)那种,于是我上万能的github上找了⼀个,找到⼀款autocreate 的代码⽣成⼯具,本⾝也是使⽤go开发的,我看了⼀下,效果还不错,并且操作⽅便简洁,⽽且还有web界⾯,clone下来之后,为了让⽣成的代码风格跟我们的项⽬保持⼀致,所以我不得不动源码,原本以为很难,但是百度了⼀下原理就是根据模板渲染。

这些就是项⽬中的模板,包括从model-router-controller-service-dao,当然这些是已经根据公司项⽬修改过的模板,开始只有⼀个controller跟dao以及model,其余的根据⾃⼰需求加就可以了,因为我想要极致的偷懒,所以直接⽣成了全套的。

第 1 页

操作这些模板的就是这个⽂件

⽂件⾥的核⼼代码,当然这也是修改过的

第 2 页

据我使⽤后得知,⾥⾯所谓的三个关键的名字只有table和module有⽤,另⼀个随便填就可以了。

修改之后,在将⽂件的⽣成⽬录修改为你项⽬的⽬录就可以了。控制⽣成⽂件⽬录的是*ContertFile⾥⾯的file变量。

全部修改后,我们就可以go run main.go运⾏起来,进⼊localhost:8081,就可以看到如下界⾯

选择其中⼀个表,就可以进⾏代码⾃动⽣成了!记住,表⼀定要有备注!表⼀定要有备注!表⼀定要有备注!不然是⽣成不了代码的。

第 3 页

这是⽣成的controller中的⼀个例⼦

⽣成之后,service中是没有逻辑的,只需要根据⾃⼰的需求增增改改就好啦,将原来的开发时间缩短了⼀半以上!再也不⽤做那些枯燥的事情!开发从建表-复制粘贴*n-增增改改-⾃测变成了建表-点⼀下-增增改改-⾃测

现在公司所有服务端的⼩伙伴都开始⽤了,我作为⼀个实习⽣,能做好这件事,其实⼼⾥的成就感也是很⼤的haha

brew install go

输入 brew info go 或者 go env 即可查看当前安装的golang版本信息

注意:千万不要把GOPATH设置成go的安装路径,可以自己在用户目录下创建一个目录,例如mygo

一般安装好go之后,使用go env查看一下当前环境。此时显示出来的GOROOT就是你使用brew安装go的安装目录,这个路径要记下来。接下来要在bash_profile文件中进行配置。

使用vim ~/.bash_profile

然后在这个文件中进行编辑,下面以我的电脑为例,路径这种要根据不同人的情况而定

GOROOT=/usr/local/Cellar/go/1.10.1/libexec (改成自己的go安装目录 go env命令可查看)

export GOROOT (不用动)

export GOPATH=/Users/jiangqiaowei/mygo (创建一个自己的文件夹 管理go相关内容)

export GOBIN=$GOPATH/bin (不用改)

export PATH=$PATH:$GOBIN:$GOROOT/bin (不用改)

$ go get golang.org/x/mobile/cmd/gomobile (需要墙)

gomobile init

$ go get -d golang.org/x/mobile/example/bind/...

$ cd $GOPATH/src/golang.org/x/mobile/example/bind

$ gomobile bind -target=ios golang.org/x/mobile/example/bind/hello

此命令在ios/路径下生成bind.xcodeprojxcode项目以及 在bind/目录下生成一个 hello.framework

拖入项目即可使用

1、解压压缩包到go工作目录,如解压到E:\opensource\go\go,解压后的目录结构如下:

E:\opensource\go\go

├─api

├─bin

│ ├─go.exe

│ ├─godoc.exe

│ └─gofmt.exe

├─doc

├─include

├─lib

├─misc

├─pkg

├─src

└─test

2、增加环境变量GOROOT,取值为上面的go工作目录

3、Path环境变量中添加"%GOROOT%\bin",以便能够直接调用go命令来编译go代码,至此go编译环境就配置好了

注:如果不想手动设置系统环境变量,也可下载go启动环境批处理附件,

修改goenv.bat文件中的GOROOT值为上面的go工作目录后直接双击该bat文件,go编译环境变量即设置完成。

4、测试go编译环境,启动一个cmd窗口,直接输入go,看到下面的提示就是搭建成功了

E:\opensource\go\go>go

Go is a tool for managing Go source code.

Usage:

go command [arguments]

The commands are:

build compile packages and dependencies

clean remove object files

doc run godoc on package sources

env print Go environment information

fix run go tool fix on packages

fmt run gofmt on package sources

get download and install packages and dependencies

install compile and install packages and dependencies

listlist packages

run compile and run Go program

testtest packages

toolrun specified go tool

version print Go version

vet run go tool vet on packages

Use "go help [command]" for more information about a command.

Additional help topics:

gopath GOPATH environment variable

packagesdescription of package lists

remote remote import path syntax

testflagdescription of testing flags

testfuncdescription of testing functions

Use "go help [topic]" for more information about that topic.

5、编译helloworld测试程序,go语言包中test目录带有helloworld.go测试程序,源码见"附一 helloworld.go",

直接调用"go build helloworld.go"就生成了"helloworld.exe"可执行程序,运行一下这个程序看到了我们期望的hello,wolrd。

E:\opensource\go\go\test>go build helloworld.go

E:\opensource\go\go\test>helloworld.exe

hello, world

E:\opensource\go\go\test>

附一 helloworld.go

// cmpout

// Copyright 2009 The Go Authors. All rights reserved.

// Use of this source code is governed by a BSD-style

// license that can be found in the LICENSE file.

// Test that we can do page 1 of the C book.

package main

func main() {

print("hello, world\n")

}