go 框架之Beego学习

写在前面:

有那么一段时间没有更新博客了,是因为上一段时间都在准备校招的面试,而上岸了一家游戏公司,所使用的编程语言也从Java改成golang了,所以一些都要从头开始学起,讲真的,golang生态真的没有Java那么好,但是因为golang的天然支持高并发,现在也是一个趋势。
在此希望所有的应届毕业生,不要限制自己的发展方向,不管是什么方向什么语言,都要有所尝试,不是你学了Java以后就一定是干Java的,编程学的是思想,不是技术!

Beego开发文档

https://www.kancloud.cn/hello123/beego/126086

前期准备

学习Beego框架之前,我们要学会golang的包管理工具,我们知道我们再安装go语言环境的时候,我们配置了 GOPATHGOROOT等等环境变量,而我们在学习的时候,也是在GOROOT的路径下面,新建一个pkg的包,把 go get XXX 的包下载到那个地方,而从go 1.11开始,开始使用 go module 方式管理我们的包

go module使用

初始化

使用 go mod init projectName 来初始化我们的工作空间,推荐自己新建一个目录(可以不是GOPATH目录下)再使用这个命令,这里命令会在当前目录下生成一个 go.mod 文件,里面记录的正是我们的包依赖关系

使用

  1. 当我们需要使用到某个包的时候,我们可以使用 go get XXX 来把我们需要的依赖包下载下来,但是下载的地方还是在 GOPATH 下面,那么我们需要使用 go mod vendor 命令来把我们下载的包复制一份到我们当前的目录下生成一个vendor文件夹,这时就可以正常使用了

  2. 当我们新clone一个项目下来需要跑,但是我们没有相关依赖的包怎么办呢?如果使用了 go module 包管理工具的话,我们可以使用 go mod download 命令来下载全部依赖

  3. 当我们下载了无用的包之后,我们想删除这个文件怎么办呢?我们可以使用 go mod tidy 删除我们在代码中没有依赖的包

  4. 还有一些命令我还没有用到过,当我们需要知道当前的指令有什么用法,或者语法规则的时候,我们可以使用 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的欢迎页面即成功启动

踩坑

  1. unable to access 'https://github.com/astaxie/beego/': OpenSSL SSL_read: Connection was reset, errno

    可能是上传大小出现了限制

    执行git config http.postBuffer 524288000git config http.sslVerify "false"

  2. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值