几年前用过beego 1.0,感觉还行,毕竟功能丰富,想要的功能基本都有。现在2.0也出来了,打算体验下。
由于beego采用mod管理依赖,首先要确保golang版本在1.5以上。
安装成功go之后,命令行 输入 go version 看看是否能正确输出版本。
任意目录创建项目文件
mkdir hello
cd hello
go mod init hello
go get github.com/beego/beego/v2@latest
此时,目录下有了mod.sum 和 go.mod两个文件,其中保存了hello这个项目的依赖关系。然后执行
go mod tidy
golang会根据go.mod文件,checkout出对应的依赖。
目录下创建文件hello.go
package main
import "github.com/beego/beego/v2/server/web"
func main() {
web.Run()
}
然后运行代码
go run hello.go
此时服务器就启动了,http://127.0.0.1:8080即可访问。
===================================================================
按照beego在github上的教程,过程中可能出现如下错误:
执行go mod init,出现如下提示
go mod init
go: cannot determine module path for source directory C:\Users\Y\Documents\code\go_project\aa (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.
原因不明,可能是初始化mod需要带上mod名称?改成 go mod init hello就可可以了。
在go get beego,并且写好hello.go之后,执行go run hello.go,会提示如下错误
$ go run aa.go
..\..\gopath\pkg\mod\github.com\beego\beego\v2@v2.0.1\server\web\staticfile.go:29:2: missing go.sum entry for module providing package github.com/hashicorp/golang-lru; to add:
go mod download github.com/hashicorp/golang-lru
..\..\gopath\pkg\mod\github.com\beego\beego\v2@v2.0.1\core\config\ini.go:31:2: missing go.sum entry for module providing package github.com/mitchellh/mapstructure; to add:
go mod download github.com/mitchellh/mapstructure
..\..\gopath\pkg\mod\github.com\beego\beego\v2@v2.0.1\core\admin\command.go:18:2: missing go.sum entry for module providing package github.com/pkg/errors; to add:
go mod download github.com/pkg/errors
..\..\gopath\pkg\mod\github.com\beego\beego\v2@v2.0.1\server\web\admin_controller.go:25:2: missing go.sum entry for module providing package github.com/prometheus/client_golang/prometheus/promhttp; to add:
go mod download github.com/prometheus/client_golang
..\..\gopath\pkg\mod\github.com\beego\beego\v2@v2.0.1\core\logs\console.go:24:2: missing go.sum entry for module providing package github.com/shiena/ansicolor; to add:
go mod download github.com/shiena/ansicolor
..\..\gopath\pkg\mod\github.com\beego\beego\v2@v2.0.1\server\web\server.go:32:2: missing go.sum entry for module providing package golang.org/x/crypto/acme/autocert; to add:
go mod download golang.org/x/crypto
..\..\gopath\pkg\mod\github.com\beego\beego\v2@v2.0.1\server\web\parser.go:31:2: missing go.sum entry for module providing package golang.org/x/tools/go/packages; to add:
go mod download golang.org/x/tools
..\..\gopath\pkg\mod\github.com\beego\beego\v2@v2.0.1\server\web\context\output.go:34:2: missing go.sum entry for module providing package gopkg.in/yaml.v2; to add:
go mod download gopkg.in/yaml.v2
大概意思是缺少go.mod入口,需要go mod命令下载相应的依赖?网上搜索如何go mod 下载全部依赖,找到答案是用 go mod tidy命令,然后就一切正常了。
附go mod常用命令:
go mod init: 初始化 go module
go mod download: 下载 go.mod 中的依赖到本地 Cache ($GOPATH/pkg/mod 下)
go mod vendor: 将项目依赖拷贝到 vendor 下
go mod tidy: 相当于 dep ensure,增加缺失的依赖(module),丢掉没用的依赖(module)
go mod verify: 校验依赖
go mod edit: 编辑依赖,通过命令行手动升级或获取依赖
go clean -modcache :清除缓存
go list -m all: 列出当前项目(main module)的构建列表
go mod graph 打印模块依赖图
go mod why 解释为什么需要依赖