不败君

前端萌新&初级后端攻城狮

安装GO并体验了一下Beego框架

安装GO并体验了一下Beego框架

2020-11-04 16:07:42

围观(4057)

安装 GO

打开官网:https://golang.org/ 或 https://studygolang.com/dl 可下载安装,如果是 Windows 系统可以直接下载安装包。

安装完成后在系统环境变量可看到 GOPATH ,可以将这个路径改为自己喜欢的位置,GO 下载的模块都会存放在这个路径。


安装 Beego

官网:beego.me

执行命令:

go get github.com/astaxie/beego

安装 Bee 工具:

go get github.com/beego/bee

将 GOPATH 下的 bin 目录放入 PATH 环境变量,例如博主的路径是:E:\soft\GOPATH\bin

此时打开命令行工具执行:

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

Use bee help [command] for more information about a command.

ADDITIONAL HELP TOPICS


Use bee help [topic] for more information about that topic.

说明 Beego 安装完成。


创建一个应用

执行:

bee new blog

即可创建一个应用,此应用存放于 GOPATH 下的 bin 目录,使用 cd 目录到 blog 目录再次执行:

bee run

返回:

______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.11.0
2020/11/04 13:30:45 WARN     ▶ 0001 Running application outside of GOPATH
2020/11/04 13:30:45 INFO     ▶ 0002 Using 'blog' as 'appname'
2020/11/04 13:30:45 INFO     ▶ 0003 Initializing watcher...

成功运行应用,此时打开浏览器并访问 http://localhost:8080 可看到 Beego 的欢迎页。


安装编辑器

安装好了 GO 和 Beego 还需要编辑器进行编码,博主使用的是 VSCODE,官网:https://code.visualstudio.com/

除了 VSCODE 也可以使用其他的 IDE ,例如 GoLand.


简单体验

打开 routers 下的 router.go 路由文件,在 init 方法写入:

beego.Router("/test", &controllers.IndexController{})

在 controllers 目录下创建一个 index.go 文件并写入:

package controllers

import (
	"github.com/astaxie/beego"
)

type IndexController struct{
	beego.Controller
}

func (this *IndexController) Get() {
	this.TplName = "index.html"
}

还需要在 view.html 编写一个 html 文件,最后打开 http://localhost/test 即可看到 index.html 的内容。


另外还有注解路由,例如在路由文件中写入:

beego.Include(&controllers.IndexController{})

控制器的方法改成这样:

// @router /test/ [get]
func (this *IndexController) Test() {
	this.TplName = "index.html"
}

此时重启 Beego 然后访问 http://localhost/test 效果同上。

注:注解路由需要 Beego 版本为 1.3+ 文档:https://beego.me/docs/mvc/controller/router.md

查看 Beego 版本的命令:

bee version

跟着文档可以很容易的进行 CRUD,和使用 PHP 的 TP 及 Laravel 框架类似,都是 MVC 架构。


常见错误

报错:

fatal: unable to access 'https://github.com/astaxie/beego/': OpenSSL SSL_connect: Connection was aborted in connection to github.com:443
package github.com/astaxie/beego: exit status 128

解决方法就是设置代理:

代理详情:https://goproxy.io/zh/

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direct

设置完成后执行命令:

go env

可查看到相关设置。

本文地址 : bubaijun.com/page.php?id=219

版权声明 : 未经允许禁止转载!

评论:我要评论

byxlle 沙发

既然来了,那就评论一下吧?喔就来看看看

评论时间:2020-11-06 00:23:55

回复

发布评论:
Copyright © 不败君 粤ICP备18102917号-1

不败君

首 页 作 品 微 语