Go framework-Beego

目录

一、Beego

1、创建Beego项目

1.1、命令行方式创建项目

1.2、使用bee工具

[1、bee api,创建API应用](#1、bee api,创建API应用)

[2、bee new 命令会自动创建beego 的go mod项目。](#2、bee new 命令会自动创建beego 的go mod项目。)

2、Admin管理后台


一、Beego

Beego用于在Go中快速开发企业应用程序,包括RESTful API、web应用程序和后端服务。

Beego 源码地址

Beego 官方站点

Beego 官方说明

Beego的特性

  • RESTful支持
  • MVC架构
  • 模块化
  • 自动API文档
  • 注释路由
  • 命名空间
  • 开发工具集合
  • Full stack for Web & API

Beego的模块

  • orm,对象关系映射
  • session,会话
  • logs,日志
  • config,配置
  • cache,缓存

1、创建Beego项目

Go Web--Go Module

Beego 官方说明

1.1、命令行方式创建项目

Go 复制代码
D:\GO_workspace_web>md test_beego

D:\GO_workspace_web>cd test_beego

D:\GO_workspace_web\test_beego>go mod init test_beego
go: creating new go.mod: module test_beego

D:\GO_workspace_web\test_beego>

下载并安装beego/v2

Go 复制代码
D:\GO_workspace_web\test_beego>go get github.com/beego/beego/v2@latest

D:\GO_workspace_web\test_beego>

下面使用GoLand软件打开新建的项目

编写main.go

Go 复制代码
package main

import "github.com/beego/beego/v2/server/web"

func main() {
	web.Run()
}

启动项目

测试:http://localhost:8080/

看到上面页面说明beego项目启动成功

1.2、使用bee工具

安装 Bee 工具

Go 复制代码
go get -u github.com/beego/bee/v2@latest

D:\GO_workspace_web\test_beego>go get -u github.com/beego/bee/v2@latest
go: downloading github.com/beego/bee/v2 v2.1.0
go: downloading github.com/beego/bee v1.12.3
go: downloading gopkg.in/yaml.v2 v2.4.0
go: downloading github.com/fsnotify/fsnotify v1.4.9
go: downloading github.com/go-delve/delve v1.20.2
go: downloading github.com/gorilla/websocket v1.4.2
go: downloading github.com/beego/beego/v2 v2.1.0
go: downloading github.com/fsnotify/fsnotify v1.6.0
go: downloading github.com/gorilla/websocket v1.5.0
go: downloading github.com/go-delve/delve v1.21.0
go: downloading github.com/beego/beego v1.12.12
go: downloading github.com/beego/beego/v2 v2.1.1
go: downloading github.com/go-sql-driver/mysql v1.7.0
go: downloading github.com/lib/pq v1.10.5
go: downloading golang.org/x/tools v0.1.12
go: downloading github.com/flosch/pongo2 v0.0.0-20200529170236-5abacdfa4915
go: downloading github.com/smartwalle/pongo2render v1.0.1
go: downloading github.com/pelletier/go-toml v1.9.2
go: downloading github.com/spf13/viper v1.7.0
go: downloading github.com/spf13/viper v1.16.0
go: downloading golang.org/x/sys v0.6.0
go: downloading github.com/go-sql-driver/mysql v1.7.1
go: downloading github.com/pkg/errors v0.9.1
go: downloading github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18
go: downloading github.com/smartwalle/pongo2render v1.0.2
go: downloading github.com/lib/pq v1.10.9
go: downloading github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3
go: downloading github.com/hashicorp/golang-lru v0.5.4
go: downloading github.com/prometheus/client_golang v1.15.1
go: downloading golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd
go: downloading github.com/hashicorp/golang-lru v1.0.2
go: downloading github.com/shiena/ansicolor v0.0.0-20230509054315-a9deabde6e02
go: downloading github.com/prometheus/client_golang v1.16.0
go: downloading github.com/cosiner/argv v0.1.0
go: downloading github.com/derekparker/trie v0.0.0-20221213183930-4c74548207f4
go: downloading github.com/go-delve/liner v1.2.3-0.20220127212407-d32d89dd2a5d
go: downloading github.com/mattn/go-colorable v0.0.9
go: downloading github.com/derekparker/trie v0.0.0-20221221181808-1424fce0c981
go: downloading github.com/mattn/go-isatty v0.0.3
go: downloading github.com/mattn/go-colorable v0.1.13
go: downloading github.com/hashicorp/hcl v1.0.0
go: downloading github.com/magiconair/properties v1.8.1
go: downloading github.com/mitchellh/mapstructure v1.5.0
go: downloading github.com/spf13/afero v1.1.2
go: downloading github.com/spf13/cast v1.3.0
go: downloading github.com/spf13/jwalterweatherman v1.0.0
go: downloading github.com/magiconair/properties v1.8.7
go: downloading github.com/spf13/pflag v1.0.5
go: downloading github.com/spf13/afero v1.9.5
go: downloading github.com/spf13/cast v1.5.1
go: downloading github.com/subosito/gotenv v1.2.0
go: downloading github.com/spf13/jwalterweatherman v1.1.0
go: downloading gopkg.in/ini.v1 v1.51.0
go: downloading github.com/subosito/gotenv v1.6.0
go: downloading gopkg.in/ini.v1 v1.67.0
go: downloading golang.org/x/net v0.7.0
go: downloading github.com/prometheus/client_model v0.3.0
go: downloading github.com/prometheus/common v0.42.0
go: downloading github.com/prometheus/client_model v0.4.0
go: downloading github.com/prometheus/common v0.44.0
go: downloading golang.org/x/arch v0.0.0-20190927153633-4e8777c89be4
go: downloading github.com/sirupsen/logrus v1.6.0
go: downloading github.com/mattn/go-runewidth v0.0.13
go: downloading github.com/sirupsen/logrus v1.9.3
go: downloading go.starlark.net v0.0.0-20220816155156-cfacd8902214
go: downloading github.com/mattn/go-runewidth v0.0.15
go: downloading golang.org/x/text v0.7.0
go: downloading github.com/google/go-dap v0.7.0
go: downloading go.starlark.net v0.0.0-20230814145427-12f4cb8177e4
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/cespare/xxhash/v2 v2.2.0
go: downloading github.com/prometheus/procfs v0.9.0
go: downloading golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4
go: downloading github.com/cespare/xxhash v1.1.0
go: downloading github.com/golang/protobuf v1.5.3
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.4
go: downloading github.com/google/go-dap v0.10.0
go: downloading github.com/prometheus/procfs v0.11.1
go: downloading github.com/cilium/ebpf v0.7.0
go: downloading github.com/konsorten/go-windows-terminal-sequences v1.0.3
go: downloading github.com/rivo/uniseg v0.2.0
go: downloading github.com/rivo/uniseg v0.4.4
go: downloading github.com/cilium/ebpf v0.11.0
go: downloading github.com/flosch/pongo2/v6 v6.0.0
go: downloading golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2
go: downloading golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
go: added github.com/beego/bee/v2 v2.1.0
go: added github.com/beego/beego/v2 v2.1.1
go: added github.com/beorn7/perks v1.0.1
go: added github.com/cespare/xxhash/v2 v2.2.0
go: added github.com/cilium/ebpf v0.11.0
go: added github.com/cosiner/argv v0.1.0
go: added github.com/davecgh/go-spew v1.1.1
go: added github.com/derekparker/trie v0.0.0-20221221181808-1424fce0c981
go: added github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3
go: added github.com/flosch/pongo2/v6 v6.0.0
go: added github.com/fsnotify/fsnotify v1.6.0
go: added github.com/go-delve/delve v1.21.0
go: added github.com/go-delve/liner v1.2.3-0.20220127212407-d32d89dd2a5d
go: added github.com/go-sql-driver/mysql v1.7.1
go: added github.com/golang/protobuf v1.5.3
go: added github.com/google/go-dap v0.10.0
go: added github.com/gorilla/websocket v1.5.0
go: added github.com/hashicorp/golang-lru v1.0.2
go: added github.com/hashicorp/hcl v1.0.0
go: added github.com/konsorten/go-windows-terminal-sequences v1.0.3
go: added github.com/lib/pq v1.10.9
go: added github.com/magiconair/properties v1.8.7
go: added github.com/mattn/go-colorable v0.1.13
go: added github.com/mattn/go-isatty v0.0.19
go: added github.com/mattn/go-runewidth v0.0.15
go: added github.com/matttproud/golang_protobuf_extensions v1.0.4
go: added github.com/mitchellh/mapstructure v1.5.0
go: added github.com/pelletier/go-toml v1.9.5
go: added github.com/pelletier/go-toml/v2 v2.0.9
go: added github.com/pkg/errors v0.9.1
go: added github.com/prometheus/client_golang v1.16.0
go: added github.com/prometheus/client_model v0.4.0
go: added github.com/prometheus/common v0.44.0
go: added github.com/prometheus/procfs v0.11.1
go: added github.com/rivo/uniseg v0.4.4
go: added github.com/shiena/ansicolor v0.0.0-20230509054315-a9deabde6e02
go: added github.com/sirupsen/logrus v1.9.3
go: added github.com/smartwalle/pongo2render v1.0.2
go: added github.com/spf13/afero v1.9.5
go: added github.com/spf13/cast v1.5.1
go: added github.com/spf13/jwalterweatherman v1.1.0
go: added github.com/spf13/pflag v1.0.5
go: added github.com/spf13/viper v1.16.0
go: added github.com/subosito/gotenv v1.6.0
go: added go.starlark.net v0.0.0-20230814145427-12f4cb8177e4
go: added golang.org/x/arch v0.4.0
go: added golang.org/x/crypto v0.12.0
go: added golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
go: added golang.org/x/mod v0.12.0
go: added golang.org/x/net v0.14.0
go: added golang.org/x/sys v0.11.0
go: added golang.org/x/text v0.12.0
go: added golang.org/x/tools v0.12.0
go: added google.golang.org/protobuf v1.31.0
go: added gopkg.in/ini.v1 v1.67.0
go: added gopkg.in/yaml.v2 v2.4.0
go: added gopkg.in/yaml.v3 v3.0.1

D:\GO_workspace_web\test_beego>

在bee命令安装:GOPATH/bin中,保证GOPATH/bin位于系统环境变量path中

bash 复制代码
D:\GO_workspace_web\test_beego>go env GOPATH
C:\Users\Administrator\go

For Go 1.18 and newer Go versions run:

Go 复制代码
go install github.com/beego/bee/v2@latest

And then:

Go 复制代码
bee version

D:\GO_workspace_web\test_beego>bee version
2023/08/17 00:10:34.189 [D]  init global config instance failed. If you do not use this, just ignore it.  open conf/app.conf: The system cannot find the path specified.
2023/08/17 00:10:34 INFO     ▶ 0001 Getting bee latest version...
2023/08/17 00:10:36 INFO     ▶ 0002 Your bee are up to date
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v2.1.0

├── GoVersion : go1.20.6
├── GOOS      : windows
├── GOARCH    : amd64
├── NumCPU    : 8
├── GOPATH    : C:\Users\Administrator\go
├── GOROOT    : C:\Program Files\Go
├── Compiler  : gc
└── Date      : Thursday, 17 Aug 2023

D:\GO_workspace_web\test_beego>

使用bee工具创建项目

1、bee api,创建API应用

Go 复制代码
D:\GO_workspace_web\test_beego>cd ..

D:\GO_workspace_web>bee api test_beego1
2023/08/17 00:21:38.410 [D]  init global config instance failed. If you do not use this, just ignore it.  open conf/app.conf: The system cannot find the path specified.
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v2.1.0
2023/08/17 00:21:38 INFO     ▶ 0001 Generate api project support go modules.
2023/08/17 00:21:38 INFO     ▶ 0002 Creating API...
        create   D:\GO_workspace_web\test_beego1\go.mod
        create   D:\GO_workspace_web\test_beego1
        create   D:\GO_workspace_web\test_beego1\conf
        create   D:\GO_workspace_web\test_beego1\controllers
        create   D:\GO_workspace_web\test_beego1\tests
        create   D:\GO_workspace_web\test_beego1\conf\app.conf
        create   D:\GO_workspace_web\test_beego1\models
        create   D:\GO_workspace_web\test_beego1\routers\
        create   D:\GO_workspace_web\test_beego1\controllers\object.go
        create   D:\GO_workspace_web\test_beego1\controllers\user.go
        create   D:\GO_workspace_web\test_beego1\tests\default_test.go
        create   D:\GO_workspace_web\test_beego1\routers\router.go
        create   D:\GO_workspace_web\test_beego1\models\object.go
        create   D:\GO_workspace_web\test_beego1\models\user.go
        create   D:\GO_workspace_web\test_beego1\main.go
2023/08/17 00:21:38 SUCCESS  ▶ 0003 New API successfully created!

D:\GO_workspace_web>

与 web 相比,不需要 html、css、js这些前端资源的响应,仅仅是业务逻辑,和数据的响应。是我们需要的典型的API功能!

2、bee new 命令会自动创建beego 的go mod项目

同时会初始化基础的目录结构!

Go 复制代码
D:\GO_workspace_web>bee new test_beego2
2023/08/17 00:24:51.524 [D]  init global config instance failed. If you do not use this, just ignore it.  open conf/app.conf: The system cannot find the path specified.
2023/08/17 00:24:51 INFO     ▶ 0001 Generate new project support go modules.
2023/08/17 00:24:51 INFO     ▶ 0002 Creating application...
        create   D:\GO_workspace_web\test_beego2\go.mod
        create   D:\GO_workspace_web\test_beego2\
        create   D:\GO_workspace_web\test_beego2\conf\
        create   D:\GO_workspace_web\test_beego2\controllers\
        create   D:\GO_workspace_web\test_beego2\models\
        create   D:\GO_workspace_web\test_beego2\routers\
        create   D:\GO_workspace_web\test_beego2\tests\
        create   D:\GO_workspace_web\test_beego2\static\
        create   D:\GO_workspace_web\test_beego2\static\js\
        create   D:\GO_workspace_web\test_beego2\static\css\
        create   D:\GO_workspace_web\test_beego2\static\img\
        create   D:\GO_workspace_web\test_beego2\views\
        create   D:\GO_workspace_web\test_beego2\conf\app.conf
        create   D:\GO_workspace_web\test_beego2\controllers\default.go
        create   D:\GO_workspace_web\test_beego2\views\index.tpl
        create   D:\GO_workspace_web\test_beego2\routers\router.go
        create   D:\GO_workspace_web\test_beego2\tests\default_test.go
        create   D:\GO_workspace_web\test_beego2\main.go
2023/08/17 00:24:52 SUCCESS  ▶ 0003 New application successfully created!

D:\GO_workspace_web>

web项目,比 api项目增加 views(html模板)和static(css,image,js)静态资源相关给目录。

运行应用前,保证全部的包被安装:

Go 复制代码
D:\GO_workspace_web\test_beego2>go mod tidy
go: downloading github.com/smartystreets/goconvey v1.6.4
go: downloading github.com/elazarl/go-bindata-assetfs v1.0.1
go: downloading github.com/stretchr/testify v1.8.1
go: downloading github.com/jtolds/gls v4.20.0+incompatible
go: downloading github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d
go: downloading github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1
go: downloading github.com/google/go-cmp v0.5.9
go: downloading gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
go: downloading github.com/kr/pretty v0.3.1
go: downloading github.com/rogpeppe/go-internal v1.10.0
go: finding module for package github.com/kr/text
go: downloading github.com/kr/text v0.2.0
go: found github.com/kr/text in github.com/kr/text v0.2.0

bee run 运行应用

Go 复制代码
D:\GO_workspace_web\test_beego2>bee run
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v2.1.0
2023/08/17 23:53:12 WARN     ▶ 0001 Running application outside of GOPATH
2023/08/17 23:53:12 INFO     ▶ 0002 Using 'test_beego2' as 'appname'
2023/08/17 23:53:12 INFO     ▶ 0003 Initializing watcher...
test_beego2/controllers
test_beego2/routers
test_beego2
2023/08/17 23:53:18 SUCCESS  ▶ 0004 Built Successfully!
2023/08/17 23:53:18 INFO     ▶ 0005 Restarting 'test_beego2.exe'...
2023/08/17 23:53:18 SUCCESS  ▶ 0006 './test_beego2.exe' is running...
2023/08/17 23:53:19.472 [I] [server.go:281]  http server Running on http://:8080

bee run 命令会利用 fsnotify 监控文件系统,若文件改动时,可以实时重新编译运行。

2、Admin管理后台

Beego 内置了管理后台,用于监视和统计应用信息,默认关闭。通过配置控制:

修改main.go文件

Go 复制代码
package main

import (
	beego "github.com/beego/beego/v2/server/web"
	_ "test_beego2/routers"
)

func main() {
	// 是否开启Admin
	beego.BConfig.Listen.EnableAdmin = true
	// Admin监听的地址和端口
	beego.BConfig.Listen.AdminAddr = "localhost"
	beego.BConfig.Listen.AdminPort = 8088
	beego.Run()
}

访问:http://localhost:8088/

支持:

Requests statistics, 请求统计信息
性能分析

  • lookup goroutine,goroutine 信息
  • lookup heap,堆栈信息
  • lookup threadcreate,线程创建信息
  • lookup block,阻塞信息
  • get cpuprof,CPU的pprof
  • get memprof,Memory的pprof
  • gc summary,GC 概要

健康检查
任务
配置状态

  • configs, 配置
  • routers, 路由
  • filters, 过滤器

Web framework-Gin

人必须要成长!

如今一道为尊,万道相和,几乎很难破。最可怕的是苍穹!

相关推荐
Theodore_10223 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
----云烟----5 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024065 小时前
SQL SELECT 语句:基础与进阶应用
开发语言
开心工作室_kaic5 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
向宇it5 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
武子康5 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神6 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
宅小海6 小时前
scala String
大数据·开发语言·scala
qq_327342736 小时前
Java实现离线身份证号码OCR识别
java·开发语言
锅包肉的九珍6 小时前
Scala的Array数组
开发语言·后端·scala