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方式下载Beego以及Bee(windows和linux都差不多)_第1张图片

  • 接着初始化刚刚最上面创建的项目
go mod init github.com/xxx/projectname

然后会在该目录下自动生成一个go.mod文件

  • 修改go.mod文件
module github.com/xxx/projectname

replace github.com/beego/bee v1.10.0 => github.com/xxx/bee v1.10.0

go 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 imports
        github.com/beego/bee/cmd imports
        github.com/beego/bee/cmd/commands/dlv imports
        github.com/derekparker/delve/service: github.com/derekparker/delve@v1.3.
1: parsing go.mod:
        module declares its path as: github.com/go-delve/delve
                but was required as: github.com/derekparker/delve

这个时候 只需要如下做就好了(后面不需要再export/set Go111MODULE=on 因为export/set这两种都只代表这一次执行)

export GO111MODULE=off && go get -u github.com/beego/bee

添加环境变量

go mod方式下载Beego以及Bee(windows和linux都差不多)_第2张图片

测试

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_design
        create   C:\GoPath\src\github.com\lujiahaoo\web_course_design\conf
        create   C:\GoPath\src\github.com\lujiahaoo\web_course_design\controllers
        create   C:\GoPath\src\github.com\lujiahaoo\web_course_design\tests
        create   C:\GoPath\src\github.com\lujiahaoo\web_course_design\conf\app.conf
        create   C:\GoPath\src\github.com\lujiahaoo\web_course_design\models
        create   C:\GoPath\src\github.com\lujiahaoo\web_course_design\routers\
        create   C:\GoPath\src\github.com\lujiahaoo\web_course_design\controllers\object.go
        create   C:\GoPath\src\github.com\lujiahaoo\web_course_design\controllers\user.go
        create   C:\GoPath\src\github.com\lujiahaoo\web_course_design\tests\default_test.go
        create   C:\GoPath\src\github.com\lujiahaoo\web_course_design\routers\router.go
        create   C:\GoPath\src\github.com\lujiahaoo\web_course_design\models\object.go
        create   C:\GoPath\src\github.com\lujiahaoo\web_course_design\models\user.go
        create   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端口
go mod方式下载Beego以及Bee(windows和linux都差不多)_第3张图片
成功

总结

在使用bee工具时

  • 提前go env -w GOPROXY=https://goproxy.cn,direct,否则会下载不了包
  • bee newbee api新建项目时取什么名,后面 go mod init就取什么名!

你可能感兴趣的:(Go,go.mod,beego,bee)