关于golang:当前流行的Go-web框架比较以及Gin介绍

9次阅读

共计 2289 个字符,预计需要花费 6 分钟才能阅读完成。

以后风行的 Go 语言 web 框架

以下是截止到 2021.10.03,GitHub 上开源的 Go Web 框架状况。目前 Gin 是遥遥领先。

Project Name Stars Forks Open Issues Description Last Commit
gin 51894 5889 443 Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance — up to 40 times faster. If you need smashing performance, get yourself some Gin. 2021-09-30 02:04:28
beego 27032 5316 27 beego is an open-source, high-performance web framework for the Go programming language. 2021-09-18 15:08:26
kit 21360 2192 47 A standard library for microservices. 2021-09-28 15:01:29
echo 20797 1841 65 High performance, minimalist Go web framework 2021-09-26 15:56:43
fasthttp 16135 1336 46 Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http 2021-10-01 11:38:31
fiber 15590 789 37 ⚡️ Express inspired web framework written in Go 2021-10-02 01:54:34
mux 15202 1413 24 A powerful HTTP router and URL matcher for building Go web servers with 🦍 2021-09-14 12:12:19
kratos 14913 3001 35 A Go framework for microservices. 2021-09-30 06:31:25
httprouter 13204 1275 63 A high performance HTTP request router that scales well 2020-09-21 13:50:23
revel 12400 1402 103 A high productivity, full-stack web framework for the Go language. 2020-07-12 05:57:36
go-zero 11533 1372 34 go-zero is a web and rpc framework written in Go. It’s born to ensure the stability of the busy sites with resilient design. Builtin goctl greatly improves the development productivity. 2021-10-02 10:16:59

Web 框架须要做什么

咱们先思考下,一个残缺的 Web 开发框架须要做哪些事件

组件 性能 是否必须
server 作为 server,监听端口,承受申请
router 路由和分组路由,能够把申请路由到对应的处理函数
middleware 反对中间件,对外部发过来的 http 申请通过中间件解决,再给到对应的处理函数。例如 http 申请的日志记录、申请鉴权 (比方校验 token)、CORS 反对、CSRF 校验等。
template engine 模板引擎,反对后端代码对 html 模板里的内容做渲染 (render),返回给前端渲染好的 html
ORM 对象关系映射,能够把代码里的对象和关系数据库的表、字段做映射关联,通过操作对象来实现数据库的增删查改等操作。

Gin 有什么

Gin 的次要作者是 Manu,Javier 和 Bo-Yi Wu,2016 年公布第一个版本,目前是最受欢迎的开源 Go 框架。

Gin 除了反对下面表格里列的 server、router、middleware 和 template 之外,还反对

  • Crash-free:解体复原,Gin 能够捕获运行期解决 http 申请过程中的 panic 并且做 recover 操作,让服务始终可用。
  • JSON validation:JSON 验证。Gin 能够解析和验证 request 里的 JSON 内容,比方字段必填等。当然开发人员也能够抉择应用第三方的 JSON validation 工具,比方 beego validation。
  • Error management:谬误治理。Gin 提供了一种简略的形式能够收集 http request 处理过程中的谬误,最终中间件能够抉择把这些谬误写入到 log 文件、数据库或者发送到其它零碎。
  • Middleware Extendtable:能够自定义中间件。Gin 除了自带的官网中间件之外,还反对用户自定义中间件,甚至能够把本人开发的中间件提交到官网代码仓库里。

Gin 自身不反对 ORM,如果想在 Gin 框架里应用 ORM,能够抉择应用第三方的 ORM,比方 gorm。

文章开源

开源地址:https://github.com/jincheng9/go-tutorial

会有一个系列分享 Go Web 开发的方方面面。

欢送大家关注公众号:coding 进阶,学习更多 Go 常识。


一起提高!

References

  • https://gin-gonic.com/
  • https://github.com/mingrammer…
正文完
 0