Gin web 框架

Gin是用Go(Golang)编写的一个网页框架。它具有类似马提尼的API,具有更好的性能,由于httprouter,速度提高了40倍。如果你需要性能和生产力,你会喜欢Gin。

 

Benchmarks

Gin uses a custom version of HttpRouter

See all benchmarks

Benchmark name (1) (2) (3) (4)
BenchmarkGin_GithubAll 30000 48375 0 0
BenchmarkAce_GithubAll 10000 134059 13792 167
BenchmarkBear_GithubAll 5000 534445 86448 943
BenchmarkBeego_GithubAll 3000 592444 74705 812
BenchmarkBone_GithubAll 200 6957308 698784 8453
BenchmarkDenco_GithubAll 10000 158819 20224 167
BenchmarkEcho_GithubAll 10000 154700 6496 203
BenchmarkGocraftWeb_GithubAll 3000 570806 131656 1686
BenchmarkGoji_GithubAll 2000 818034 56112 334
BenchmarkGojiv2_GithubAll 2000 1213973 274768 3712
BenchmarkGoJsonRest_GithubAll 2000 785796 134371 2737
BenchmarkGoRestful_GithubAll 300 5238188 689672 4519
BenchmarkGorillaMux_GithubAll 100 10257726 211840 2272
BenchmarkHttpRouter_GithubAll 20000 105414 13792 167
BenchmarkHttpTreeMux_GithubAll 10000 319934 65856 671
BenchmarkKocha_GithubAll 10000 209442 23304 843
BenchmarkLARS_GithubAll 20000 62565 0 0
BenchmarkMacaron_GithubAll 2000 1161270 204194 2000
BenchmarkMartini_GithubAll 200 9991713 226549 2325
BenchmarkPat_GithubAll 200 5590793 1499568 27435
BenchmarkPossum_GithubAll 10000 319768 84448 609
BenchmarkR2router_GithubAll 10000 305134 77328 979
BenchmarkRivet_GithubAll 10000 132134 16272 167
BenchmarkTango_GithubAll 3000 552754 63826 1618
BenchmarkTigerTonic_GithubAll 1000 1439483 239104 5374
BenchmarkTraffic_GithubAll 100 11383067 2659329 21848
BenchmarkVulcan_GithubAll 5000 394253 19894 609
  • (1):总重复次数达到的时间越长,意味着越有信心的结果
  • (2):单次重复持续时间(ns / op),越低越好
  • (3):堆内存(B / op),越低越好
  • (4):每个重复的平均分配(分配/操作),越低越好

Gin v1. stable

  •  零分配路由器。
  • 仍然是最快的http路由器和框架。从路由到写作。
  •  完整的单元测试套件
  •  测试战斗
  •  API冻结,新版本不会破坏你的代码。

开始使用它

  1. 下载并安装它

  1. 在你的代码中导入它:

  1. (可选)导入net/http。例如,如果使用常量如http.StatusOK

使用像Govendor这样的供应商工具

  1. go get govendor

  1. 创建你的项目文件夹cd到里面

  1. Vendor init your project and add gin

  1. 在项目中复制起始模板

  1. Run your project

jsoniter构建

Ginencoding/json用作默认的json包,但你可以通过从其他标签建立更改为jsoniter

API Examples

Using GET, POST, PUT, PATCH, DELETE and OPTIONS

路径中的参数

查询字符串参数

Multipart/Urlencoded Form

Another example: query + post form

Upload files

单个文件

引用问题#774和详细示例代码

How to curl:

Multiple files

查看详细的示例代码

How to curl:

Grouping routes

没有中间件的默认空白Gin

使用

代替

使用中间件

如何写日志文件

模型绑定和验证

要将请求主体绑定到一个类型,使用模型绑定。我们目前支持绑定JSON,XML和标准表单值(foo = bar&boo = baz)。

杜松子酒使用go-playground / validator.v8进行验证。在这里查看关于标签使用情况的完整文档。

请注意,您需要在要绑定的所有字段上设置相应的绑定标签。例如,从JSON绑定时,设置json:"fieldname"

另外,杜松子提供了两套绑定方法:

  • 类型 – 必须绑定
    • 方法 – ,,BindBindJSONBindQuery
    • 行为 – 这些方法MustBindWith在引擎盖下使用。如果存在绑定错误,则请求被中止c.AbortWithError(400, err).SetType(ErrorTypeBind)。这将响应状态码设置为400,并将Content-Type标题设置为text/plain; charset=utf-8。请注意,如果在此之后尝试设置响应代码,将会导致警告[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 422。如果您希望更好地控制行为,请考虑使用ShouldBind等效的方法。
  • 类型 – 应该绑定
    • 方法 – ,,ShouldBindShouldBindJSONShouldBindQuery
    • 行为 – 这些方法ShouldBindWith在引擎盖下使用。如果发生绑定错误,则返回错误,开发人员有责任正确处理请求和错误。

当使用绑定方法时,杜松子试图根据Content-Type头来推断活页夹。如果你确定你是绑定的,你可以使用MustBindWithShouldBindWith

您也可以指定特定字段是必需的。如果一个字段装饰binding:"required"并绑定时有一个空值,将返回一个错误。

Sample request

自定义验证器

也可以注册自定义验证器。请参阅示例代码

只绑定查询字符串

ShouldBindQuery函数只绑定查询参数,而不是发布数据。查看详细信息

绑定查询字符串或发布数据

查看详细信息

Test it with:

绑定HTML复选框

查看详细信息

main.go

form.html

result:

Multipart/Urlencoded binding

Test it with:

XML,JSON和YAML (rendering)渲染

SecureJSON

使用SecureJSON来防止json劫持。"while(1),"如果给定的结构体是数组值,那么缺省前置于响应主体。

提供静态文件

HTML (rendering)渲染

使用LoadHTMLGlob()或LoadHTMLFiles()

templates/index.tmpl

在不同的目录中使用同名的模板

templates/posts/index.tmpl

自定义模板渲染器

你也可以使用你自己的html模板渲染

自定义分隔符

您可以使用自定义分隔符

自定义模板功能

查看详细的示例代码

main.go

raw.tmpl

Result:

Multitemplate

Gin允许默认只使用一个html.Template。检查使用功能的多模板渲染,如go 1.6 block template

重定向

发出HTTP重定向很简单:

内部和外部位置均受支持。

自定义中间件

使用BasicAuth()中间件

Goroutines在一个中间件里面

在中间件或处理程序中启动新的Goroutine时,不应使用其中的原始上下文,而必须使用只读副本。

自定义HTTP配置

http.ListenAndServe()直接使用,如下所示:

支持让我们加密

1行LetsEncrypt HTTPS服务器的示例。

自定义autocert管理器的例子。

使用Gin运行多个服务

查看问题并尝试以下示例:

优雅的重启或停止

你想优雅地重新启动或停止你的网络服务器?有一些办法可以做到。

我们可以使用fvbock / endless来替换默认值ListenAndServe。有关更多详细信息,请参阅问题#296

无止境的替代:

  • 礼貌:礼貌的Go HTTP服务器,优雅地关闭。
  • 优雅:优雅是一个Go包,可以正常关闭http.Handler服务器。
  • 宽限期:Go服务器的平稳重启和零宕机部署。

如果你使用的是Go 1.8,你可能不需要使用这个库。考虑使用http.Server内置的Shutdown()方法来正常关闭。用杜松子酒查看完整的关机示例。

测试

net/http/httptest包是HTTP测试的首选方式。

测试上面的代码示例:

用户 Sourcegraph

令人敬畏的项目列表使用Gin网络框架。

  • drone: Drone is a Continuous Delivery platform built on Docker, written in Go
  • gorush: A push notification server written in Go.