go beego 环境搭建
1.下载go安装包,安装
https://golang.google.cn/dl/
2. 设置环境
重点:解决国内下载go依赖包慢的问题
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,https://goproxy.cn,direct
有时下不来依赖包,可以尝试以下命令
go env -w GO111MODULE=on
go env -w GO111MODULE=auto
3.下载beego 框架 ,bee 工具
注意:下载beego@v1.12.1,现在的beego@v1.12.2有问题
go get github.com/astaxie/beego
go get github.com/beego/bee
上面下载慢,怎么办
git clone https://github.com/astaxie/beego.git
然后拷贝到 $GOPATH/src/github.com/astaxie/beego 目录
git clone https://github.com/beego/bee.git
然后拷贝到 $GOPATH/src/github.com/beego/bee 目录
然后通过本地执行安装:
cd C:\Users\lenovo\go\src\github.com\astaxie\beego
go install
cd C:\Users\lenovo\go\src\github.com\beego\bee
go install
4.自动生成beego项目
bee new myapp
cd myapp
bee run myapp
你可以在conf文件里修改项目的端口号
5.创建一个API项目
可以执行如下命令
bee api beeapi
cd beeapi
bee run -downdoc=true -gendoc=true
在浏览器中输入http://127.0.0.1:8080/swagger/
6.如果项目目录下没有go.mod文件,需要生成
go mod init
7.通过beego快速创建一个Restful风格API项目及API文档自动化
a.创建数据库及数据表(MySQL)
b.通过数据库创建Restful风格API项目
mkdir testdev
cd testdev
mkdir src
cd src
bee api testdev-api -conn="root:mima@tcp(127.0.0.1:3306)/testdev"
c.API文档自动化
bee generate docs
bee run watchall true
访问 <http://127.0.0.1:8080/swagger/ 即可看到自动化生产的API文档
bee run -downdoc=true
该命令会监测swagger目录下面是否有swagger-ui的文件,如果没有就从github上面下载,下载的地址是: https://github.com/beego/swagger/archive/v2.zip
bee run -gendoc=true
这样可以在每次项目重新运行的时候更新api文档,不用重新运行命令:bee generate docs。

浙公网安备 33010602011771号