Go Web Framework比较

前言

Go语言诞生快十年了,已经不能说是一门新语言了。现在涌现出来出来非常多的Web框架。今天就选取其中几个来比较一下

今天选取的是三个FullStack框架,也就是啥都能干的框架

beego

这个是国产的开源框架

先来看看怎么启动的

主程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main

import (
"hello/controllers"

"github.com/astaxie/beego"
)

func main() {
beego.BConfig.RunMode = "prod"
beego.Router("/json", &controllers.JsonController{})
beego.Router("/plaintext", &controllers.PlaintextController{})
beego.Run()
}

主程序主要是设置配置,配置路由和启动服务

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package controllers

type PlaintextController struct {
Base
}

const helloWorldString = "Hello, World!"

var (
helloWorldBytes = []byte(helloWorldString)
)

func (c *PlaintextController) Get() {
c.Ctx.Output.Header("Content-Type", "text/plain")
c.Ctx.Output.Body(helloWorldBytes)
}

控制器负责处理请求

整体看下来,封装的应该比较简洁,但是ctx这块感觉不太雅观

revel

revel比较像Java和Scala的Play Framework

revel这个框架封装的比较深,看官方的例子需要使用提供的revel命令行才能使用,虽然beego也提供了bee工具,但是也可以不使用

1
2
3
4
5
6
# 下载库
go get github.com/revel/revel
# 安装命令行
go get github.com/revel/cmd/revel
# 导出到环境变量
export PATH="$PATH:$GOPATH/bin"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
~
~ revel! http://revel.github.io
~
usage: revel command [arguments]

The commands are:

new create a skeleton Revel application
run run a Revel application
build build a Revel application (e.g. for deployment)
package package a Revel application (e.g. for deployment)
clean clean a Revel application's temp files
test run all tests from the command-line
version displays the Revel Framework and Go version

先新建一个项目

1
2
revel new demo
revel run demo

然后访问http://localhost:9000

这样看revel使用起来比较方便

路由是在单独的文件里面配置的,在/conf/routes,然后revel会自动生成routers.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Routes Config
#
# This file defines all application routes (Higher priority routes first)
#

module:testrunner
# module:jobs


GET / App.Index

# Ignore favicon requests
GET /favicon.ico 404

# Map static resources from the /app/public folder to the /public path
GET /public/*filepath Static.Serve("public")

# Catch all, this will route any request into the controller path
#
# **** WARNING ****
# Enabling this exposes any controller and function to the web.
# ** This is a serious security issue if used online **
#
# For rapid development uncomment the following to add new controller.action endpoints
# without having to add them to the routes table.
# * /:controller/:action :controller.:action

真正的处理请求在app.go

1
2
3
4
5
6
7
8
9
10
11
12
13
package controllers

import (
"github.com/revel/revel"
)

type App struct {
*revel.Controller
}

func (c App) Index() revel.Result {
return c.Render()
}

封装的确实比较深了,但是开发起来会很方便,就是自定义话成都不高

aah

安装工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
go get -u aahframework.org/tools.v0/aah

aah

‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
aah framework v0.10 - https://aahframework.org
____________________________________________________________________
# Report improvements/bugs at https://github.com/go-aah/aah/issues #

Usage:
aah [global options] command [command options] [arguments...]

Commands:
new, n Create new aah 'web' or 'api' application (interactive)
run, r Run aah framework application (supports hot-reload)
build, b Build aah application for deployment
list, l List all aah projects in GOPATH
clean, c Cleans the aah generated files and build directory
switch, s Switch between aah release and edge version
update, u Update your aah to the latest release version on your GOPATH
generate, g Generates boilerplate code, configurations, complement scripts (systemd, docker), etc.
help, h Shows a list of commands or help for one command

Global Options:
-h, --help show help
-v, --version print aah framework version and go version

这三个框架都提供了命令行工具,方便了开发部署

aah也是通过配置文件来配置路由

config/routes.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
routes {
...
#------------------------------------------------------
# Pick an unique name, it's called `route name`,
# used for reverse URL.
#------------------------------------------------------
index {
# path is used to match incoming requests
# It can contain `:name` - Named parameter and
# `*name` - Catch-all parameter
path = "/"

# HTTP method mapping, It can be multiple `HTTP` methods with comma separated
# Default value is `GET`, it can be lowercase or uppercase
#method = "GET"

# The controller to be called for mapped URL path.
# * `controller` attribute supports with or without package prefix. For e.g.: `v1/User` or `User`
# * `controller` attribute supports both naming conventions. For e.g.: `User` or `UserController`
controller = "AppController"
...

控制类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package controllers

import (
"aahframework.org/aah.v0"
"sample/app/models"
)

// AppController struct application controller
type AppController struct {
*aah.Context
}

// Index method is application home page.
func (a *AppController) Index() {
data := aah.Data{
"Greet": models.Greet{
Message: "Welcome to aah framework - Web Application",
},
}

a.Reply().Ok().HTML(data)
}

Benchmark

这是一个第三方的测试结果

Web Framework JSON Plaintext
beego 334640 638004
revel 249814 215532
aah 181207 215532

从表格中可以看出beego的性能是最好的,不过几个框架的性能差别都不算特别大,性能也不是选择这几个框架的主要原因

总结

共同点

  1. 三个框架都是Fullstack框架,提供一站式解决方案
  2. 都提供了工具开发、测试、部署,但revel和aah集成度似乎更高,不能单独使用
  3. 性能方面相差不大,而且单机查询性能也能上十万,应该不会是瓶颈。go的性能还是有保障的

不同点

  1. beego类似传统的mvc框架
  2. revel和aah比较类似,都学的play framework

选择推荐

  1. 既然选择全栈开发框架,显然是为了满足展开发效率,beego是国人开发具有较好的中文文档,英文不太好的首选beego、
  2. revel提供了更为彻底的一站式解决方案,而且也比较成熟,推荐选择
  3. aah应该来说算是后起之秀,截止本文撰写之日在Github只有304个starts。比较适合尝鲜