Gin之gin介绍和安装

1、gin介绍

1.1 gin 是什么?

Gin 是一个用 Go (Golang) 编写的 HTTP web 框架。 它是一个类似于 martini 但拥有更好性能的 API 框架,由于

httprouter,速度提高了近 40 倍。如果你需要极好的性能,使用 Gin 吧。

https://github.com/go-martini/martini

https://github.com/julienschmidt/httprouter

1.2 gin优点?

  • 快速

    基于 Radix 树的路由,小内存占用。没有反射。可预测的 API 性能。

  • 支持中间件

    传入的 HTTP 请求可以由一系列中间件和最终操作来处理。例如:Logger,Authorization,GZIP,最终操作

    DB。

  • Crash 处理

    Gin 可以 catch 一个发生在 HTTP 请求中的 panic 并 recover 它。这样,你的服务器将始终可用。例如,你可

    以向 Sentry 报告这个 panic!

  • JSON 验证

    Gin 可以解析并验证请求的 JSON,例如检查所需值的存在。

  • 路由组

    更好地组织路由。是否需要授权,不同的 API 版本…… 此外,这些组可以无限制地嵌套而不会降低性能。

  • 错误管理

    Gin 提供了一种方便的方法来收集 HTTP 请求期间发生的所有错误。最终,中间件可以将它们写入日志文件,

    数据库并通过网络发送。

  • 内置渲染

    Gin 为 JSON,XML 和 HTML 渲染提供了易于使用的 API。

  • 可扩展性

    新建一个中间件非常简单。

1.3 gin特性?

Gin v1 稳定的特性:

  • 零分配路由。
  • 仍然是最快的 http 路由器和框架。
  • 完整的单元测试支持。
  • 实战考验。
  • API 冻结,新版本的发布不会破坏你的代码。

1.4 基准测试

Gin 使用了自定义版本的 HttpRouter。

查看所有基准测试:https://github.com/gin-gonic/gin/blob/master/BENCHMARKS.md

Benchmark name(1)(2)(3)(4)
BenchmarkGin_GithubAll300004837500
BenchmarkAce_GithubAll1000013405913792167
BenchmarkBear_GithubAll500053444586448943
BenchmarkBeego_GithubAll300059244474705812
BenchmarkBone_GithubAll20069573086987848453
BenchmarkDenco_GithubAll1000015881920224167
BenchmarkEcho_GithubAll100001547006496203
BenchmarkGocraftWeb_GithubAll30005708061316561686
BenchmarkGoji_GithubAll200081803456112334
BenchmarkGojiv2_GithubAll200012139732747683712
BenchmarkGoJsonRest_GithubAll20007857961343712737
BenchmarkGoRestful_GithubAll30052381886896724519
BenchmarkGorillaMux_GithubAll100102577262118402272
BenchmarkHttpRouter_GithubAll2000010541413792167
BenchmarkHttpTreeMux_GithubAll1000031993465856671
BenchmarkKocha_GithubAll1000020944223304843
BenchmarkLARS_GithubAll200006256500
BenchmarkMacaron_GithubAll200011612702041942000
BenchmarkMartini_GithubAll20099917132265492325
BenchmarkPat_GithubAll2005590793149956827435
BenchmarkPossum_GithubAll1000031976884448609
BenchmarkR2router_GithubAll1000030513477328979
BenchmarkRivet_GithubAll1000013213416272167
BenchmarkTango_GithubAll3000552754638261618
BenchmarkTigerTonic_GithubAll100014394832391045374
BenchmarkTraffic_GithubAll10011383067265932921848
BenchmarkVulcan_GithubAll500039425319894609

(1)、在一定的时间内实现的总调用数,越高越好

(2)、单次操作耗时(ns/op),越低越好

(3)、堆内存分配 (B/op), 越低越好

(4)、每次操作的平均内存分配次数(allocs/op),越低越好

1.5 参考

Gin HitHub:https://github.com/gin-gonic/gin/

使用例子:https://github.com/gin-gonic/examples

1.6 用户

使用 Gin web 框架的知名项目:

  • gorush:Go 编写的通知推送服务器。
  • fnproject:原生容器,云 serverless 平台。
  • photoprism:由 Go 和 Google TensorFlow 提供支持的个人照片管理工具。
  • krakend:拥有中间件的超高性能 API 网关。
  • picfit:Go 编写的图像尺寸调整服务器。
  • gotify:使用实时 web socket 做消息收发的简单服务器。
  • cds:企业级持续交付和 DevOps 自动化开源平台。

2、gin安装

2.1 要求

Go 1.13 及以上版本

2.2 安装

要安装 Gin 软件包,需要先安装 Go 并设置 Go 工作区。

1、下载并安装 gin:

$ go get -u github.com/gin-gonic/gin

2、将 gin 引入到代码中:

import "github.com/gin-gonic/gin"

3、(可选)如果使用诸如 http.StatusOK 之类的常量,则需要引入 net/http 包:

import "net/http"
  • 24
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值