写在前面:
有那么一段时间没有更新博客了,是因为上一段时间都在准备校招的面试,而上岸了一家游戏公司,所使用的编程语言也从Java改成golang了,所以一些都要从头开始学起,讲真的,golang生态真的没有Java那么好,但是因为golang的天然支持高并发,现在也是一个趋势。
在此希望所有的应届毕业生,不要限制自己的发展方向,不管是什么方向什么语言,都要有所尝试,不是你学了Java以后就一定是干Java的,编程学的是思想,不是技术!
Beego开发文档
https://www.kancloud.cn/hello123/beego/126086
前期准备
学习Beego框架之前,我们要学会golang的包管理工具,我们知道我们再安装go语言环境的时候,我们配置了 GOPATH
、GOROOT
等等环境变量,而我们在学习的时候,也是在GOROOT
的路径下面,新建一个pkg的包,把 go get XXX
的包下载到那个地方,而从go 1.11开始,开始使用 go module
方式管理我们的包
go module使用
初始化
使用 go mod init projectName
来初始化我们的工作空间,推荐自己新建一个目录(可以不是GOPATH目录下)再使用这个命令,这里命令会在当前目录下生成一个 go.mod
文件,里面记录的正是我们的包依赖关系
使用
-
当我们需要使用到某个包的时候,我们可以使用
go get XXX
来把我们需要的依赖包下载下来,但是下载的地方还是在GOPATH
下面,那么我们需要使用go mod vendor
命令来把我们下载的包复制一份到我们当前的目录下生成一个vendor
文件夹,这时就可以正常使用了 -
当我们新clone一个项目下来需要跑,但是我们没有相关依赖的包怎么办呢?如果使用了
go module
包管理工具的话,我们可以使用go mod download
命令来下载全部依赖 -
当我们下载了无用的包之后,我们想删除这个文件怎么办呢?我们可以使用
go mod tidy
删除我们在代码中没有依赖的包 -
还有一些命令我还没有用到过,当我们需要知道当前的指令有什么用法,或者语法规则的时候,我们可以使用
go help mod
命令来查看(这一点很有用,务必学会)Go mod provides access to operations on modules. Note that support for modules is built into all the go commands, not just 'go mod'. For example, day-to-day adding, removing, upgrading, and downgrading of dependencies should be done using 'go get'. See 'go help modules' for an overview of module functionality. Usage: go mod <command> [arguments] The commands are: download download modules to local cache edit edit go.mod from tools or scripts graph print module requirement graph init initialize new module in current directory tidy add missing and remove unused modules vendor make vendored copy of dependencies verify verify dependencies have expected content why explain why packages or modules are needed Use "go help mod <command>" for more information about a command.
快速入门
我们可以使用bee脚手架来快速搭建我们的beego框架
bee
安装
go get github.com/beego/bee
安装脚手架,可能要花费一些时间,等一下就好了
验证
安装好bee框架之后,我们可以在命令行输入 bee 来查看,如果安装成功,则会出现如下的信息
Bee is a Fast and Flexible tool for managing your Beego Web Application.
USAGE
bee command [arguments]
AVAILABLE COMMANDS
version Prints the current Bee version
migrate Runs database migrations
api Creates a Beego API application
bale Transforms non-Go files to Go source files
fix Fixes your application by making it compatible with newer versions of Beego
pro Source code generator
dlv Start a debugging session using Delve
dockerize Generates a Dockerfile for your Beego application
generate Source code generator
hprose Creates an RPC application based on Hprose and Beego frameworks
new Creates a Beego application
pack Compresses a Beego application into a single file
rs Run customized scripts
run Run the application by starting a local development server
server serving static content over HTTP on port
update Update Bee
Use bee help [command] for more information about a command.
ADDITIONAL HELP TOPICS
Use bee help [topic] for more information about that topic.
快速搭建项目
通过命令行提示,我们可以知道,使用 bee new projectName
来初始化一个项目。其实可能要下载一些包,我们需要等待一下,然后我们使用bee run
的命令来启动我们的项目,通过浏览器输入localhost:8080
可以看到beego的欢迎页面即成功启动
踩坑
-
unable to access 'https://github.com/astaxie/beego/': OpenSSL SSL_read: Connection was reset, errno
可能是上传大小出现了限制
执行
git config http.postBuffer 524288000
和git config http.sslVerify "false"
-
A connection attempt failed because the connected party did not
执行
go env
查看,有一项叫做set GOSUMDB=sum.golang.org
的,可能是因为有墙,我们把它关掉go env -w GOSUMDB=off
,代理推荐go env -w GOPROXY=https://goproxy.cn,direct