导航

GOPATH之外新建beego项目

Posted on 2022-07-28 15:36  蝈蝈俊  阅读(216)  评论(0编辑  收藏  举报

配置go mod

Go 1.11,可以将项目放在 GOPATH 之外, 下面的过程就是这样场景的项目建立过程。

$ go mod init archmeasure
go: creating new go.mod: module archmeasure
go: to add module requirements and sums:
        go mod tidy

注意,GOPATH 之外建立项目时, module path 必须有。否则会下面错误

$ go mod init 
go: cannot determine module path for source directory /Users/guohongjun/Documents/MyCodes/arch-measure (outside GOPATH, module path must be specified)

Example usage:
        'go mod init example.com/m' to initialize a v0 or v1 module
        'go mod init example.com/m/v2' to initialize a v2 module

Run 'go help mod init' for more information.

安装或者升级 Beego 和 Bee 的开发工具

$ go get -u github.com/beego/beego/v2
$ go get -u github.com/beego/bee/v2

使用 bee 新建项目

bee new website
2022/07/28 15:03:45 INFO     ▶ 0001 Getting bee latest version...
2022/07/28 15:03:45 WARN     ▶ 0002 Update available 2.0.2 ==> 2.0.4
2022/07/28 15:03:45 WARN     ▶ 0003 Run `bee update` to update
2022/07/28 15:03:45 INFO     ▶ 0004 Your bee are up to date
2022/07/28 15:03:45 INFO     ▶ 0005 generate new project support go modules.
2022/07/28 15:03:45 INFO     ▶ 0006 Creating application...
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/go.mod
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/conf/
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/controllers/
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/models/
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/routers/
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/tests/
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/static/
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/static/js/
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/static/css/
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/static/img/
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/views/
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/conf/app.conf
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/controllers/default.go
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/views/index.tpl
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/routers/router.go
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/tests/default_test.go
        create   /Users/guohongjun/Documents/MyCodes/arch-measure/website/main.go
2022/07/28 15:03:45 SUCCESS  ▶ 0007 New application successfully created!

bee 的工具还没兼容GOPATH之外新建项目,需要新建完成后做下面修改:

  • 只用一个go.mod , 如果beego不是唯一的项目,需要删除bee默认新建的 go.mod 。
  • import 的路径上增加 module path, 比如 默认是 import _ "website/routers" 需要改成 import _ "archmeasure/website/routers"
  • default_test.go 中 trace 需要改 import "github.com/beego/beego/v2/core/logs"beego.Trace 改成 logs.Trace