本文主要是介绍go mod方式下载Beego以及Bee(windows和linux都差不多),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
更新
现在代理直接使用 go env -w GOPROXY=https://goproxy.cn,direct
就好了,替换掉之前总是 set
(export)
必要准备
首先需要下载git(下一个gitbash就好了)
其次需要一个github账号
创建项目文件夹
mkdir go_project
cd go_project
go env -w GOPROXY=https://goproxy.cn,direct
初始化项目并替换Bee源
为什么要替换呢?下面是作者的原话
you’ll need to clone it over the original repo (that is best anyways, so you don’t have to refactor any code when this gets released in the original repo). Hope this works for you 😃:
意思就是不怕版本的变化对你bee 工具的影响
- 首先将
github.com/beego/bee
fork到自己的仓库下
- 接着初始化刚刚最上面创建的项目
go mod init github.com/xxx/projectname
然后会在该目录下自动生成一个go.mod
文件
- 修改
go.mod
文件
module github.com/xxx/projectnamereplace github.com/beego/bee v1.10.0 => github.com/xxx/bee v1.10.0go 1.13
安装beego 和 bee
go get -u github.com/astaxie/beego
go get -u github.com/beego/bee
坑来了
在在下载beego
的时候还是正常的,下载bee
的时候,会报错
go: github.com/beego/bee importsgithub.com/beego/bee/cmd importsgithub.com/beego/bee/cmd/commands/dlv importsgithub.com/derekparker/delve/service: github.com/derekparker/delve@v1.3.
1: parsing go.mod:module declares its path as: github.com/go-delve/delvebut was required as: github.com/derekparker/delve
这个时候 只需要如下做就好了(后面不需要再export/set Go111MODULE=on 因为export/set这两种都只代表这一次执行)
export GO111MODULE=off && go get -u github.com/beego/bee
添加环境变量
测试
用 git bash
进入项目并创建
cd go_project
bee api github.com/xxx/web_course_design
会看到
FATAL ▶ 0001 There was an error running 'go version' command: exec:
"go": executable file not found in %PATH%
需要关闭gitbash
窗口,用cmd管理员方式
创建
C:\Users\xxx>cd /go_project
bee api github.com/xxx/web_course_design
显示
C:\go_project\web_course_design>
bee api github.com/xxx/web_course_design
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v1.10.0
2019/09/21 00:01:04 WARN ▶ 0001 You current workdir is not inside $GOPATH/src.
2019/09/21 00:01:04 INFO ▶ 0002 Creating API...create C:\GoPath\src\github.com\lujiahaoo\web_course_designcreate C:\GoPath\src\github.com\lujiahaoo\web_course_design\confcreate C:\GoPath\src\github.com\lujiahaoo\web_course_design\controllerscreate C:\GoPath\src\github.com\lujiahaoo\web_course_design\testscreate C:\GoPath\src\github.com\lujiahaoo\web_course_design\conf\app.confcreate C:\GoPath\src\github.com\lujiahaoo\web_course_design\modelscreate C:\GoPath\src\github.com\lujiahaoo\web_course_design\routers\create C:\GoPath\src\github.com\lujiahaoo\web_course_design\controllers\object.gocreate C:\GoPath\src\github.com\lujiahaoo\web_course_design\controllers\user.gocreate C:\GoPath\src\github.com\lujiahaoo\web_course_design\tests\default_test.gocreate C:\GoPath\src\github.com\lujiahaoo\web_course_design\routers\router.gocreate C:\GoPath\src\github.com\lujiahaoo\web_course_design\models\object.gocreate C:\GoPath\src\github.com\lujiahaoo\web_course_design\models\user.gocreate C:\GoPath\src\github.com\lujiahaoo\web_course_design\main.go
2019/09/21 00:01:04 SUCCESS ▶ 0003 New API successfully created!
坑又来了
目前bee生成的项目只能安装在$GOPATH/src
下的。。并不会安装在你刚刚创建的go_project
下,此时只能在 dos
下进入$GOPATH/src/projectname
,我这里项目是在github.com
目录下的
$GOPATH/src/github.com/web_course_design
下,然后执行
go mod init github.com/xxx/web_course_design
这个时候使用bee run
才成功。。。
C:\GoPath\src\github.com\lujiahaoo\web_course_design>bee run
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v1.10.0
2019/09/21 00:02:48 INFO ▶ 0001 Using 'web_course_design' as 'appname'
2019/09/21 00:02:48 INFO ▶ 0002 Initializing watcher...
go: finding github.com/shiena/ansicolor latest
github.com/lujiahaoo/web_course_design/models
github.com/lujiahaoo/web_course_design/controllers
github.com/lujiahaoo/web_course_design/routers
github.com/lujiahaoo/web_course_design
2019/09/21 00:02:57 SUCCESS ▶ 0003 Built Successfully!
2019/09/21 00:02:57 INFO ▶ 0004 Restarting 'web_course_design.exe'...
2019/09/21 00:02:57 SUCCESS ▶ 0005 './web_course_design.exe' is running...
2019/09/21 00:02:58.662 [I] [parser.go:112] generate router from comments
2019/09/21 00:02:58.703 [I] [router.go:270] C:\GoPath\src\github.com\lujiahaoo\web_course_design\controllers no changed
2019/09/21 00:02:58.851 [I] [asm_amd64.s:1357] http server Running on http://:8080
2019/09/21 00:03:07.626 [D] [server.go:2802] | 127.0.0.1| 404 | 1.9946ms| nomatch| GET /
浏览器访问 8080端口
成功
总结
在使用bee
工具时
- 提前
go env -w GOPROXY=https://goproxy.cn,direct
,否则会下载不了包 - 在
bee new
或bee api
新建项目时取什么名,后面go mod init
就取什么名!
这篇关于go mod方式下载Beego以及Bee(windows和linux都差不多)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!