Skip to content

Gin Web Framework

Gin是一個使用Go語言編寫的Web框架。它具有類似martini的API,並且由於使用了httprouter,性能高出40倍。如果您需要高性能和良好的生產力,您會喜歡上Gin。

Gin的主要特點包括:

  • 零分配路由器
  • 快速
  • 中間件支持
  • 無崩潰
  • JSON驗證
  • 路由分組
  • 錯誤管理
  • 內置渲染
  • 可擴展

入門指南

先決條件

  • Go:任何最新的三個主要版本 發布 (我們通過這些版本進行測試)。

獲取Gin

使用Go模塊支持,只需將以下import添加到您的代碼中

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

然後運行 go [build|run|test],將自動提取必要的依賴。

否則,運行以下Go命令安裝gin套件:

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

運行Gin

首先您需要導入Gin套件以使用Gin,下面是一個最簡單的示例 example.go

go
package main

import (
  "net/http"

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

func main() {
  r := gin.Default()
  r.GET("/ping", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{
      "message": "pong",
    })
  })
  r.Run() // 監聽並在0.0.0.0:8080上提供服務 (對於Windows為 "localhost:8080")
}

然後使用以下Go命令運行示例:

sh
# 運行 example.go 並在瀏覽器上訪問 0.0.0.0:8080/ping
$ go run example.go

學習更多示例

示例

Gin examples 倉庫中有許多準備好的示例,展示了Gin的各種用例。

文檔

請參閱API文檔和描述 以獲取套件信息。

所有文檔都可在Gin網站上找到。

關於Gin的文章

一個精選的Gin框架優秀文章列表。

基準測試

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

基準測試名稱(1)(2)(3)(4)
BenchmarkGin_GithubAll4355027364 ns/op0 B/op0 allocs/op
BenchmarkAce_GithubAll4054329670 ns/op0 B/op0 allocs/op
BenchmarkAero_GithubAll5763220648 ns/op0 B/op0 allocs/op
BenchmarkBear_GithubAll9234216179 ns/op86448 B/op943 allocs/op
BenchmarkBeego_GithubAll7407243496 ns/op71456 B/op609 allocs/op
BenchmarkBone_GithubAll4202922835 ns/op720160 B/op8620 allocs/op
BenchmarkChi_GithubAll7620238331 ns/op87696 B/op609 allocs/op
BenchmarkDenco_GithubAll1835564494 ns/op20224 B/op167 allocs/op
BenchmarkEcho_GithubAll3125138479 ns/op0 B/op0 allocs/op
BenchmarkGocraftWeb_GithubAll4117300062 ns/op131656 B/op1686 allocs/op
BenchmarkGoji_GithubAll3274416158 ns/op56112 B/op334 allocs/op
BenchmarkGojiv2_GithubAll1402870518 ns/op352720 B/op4321 allocs/op
BenchmarkGoJsonRest_GithubAll2976401507 ns/op134371 B/op2737 allocs/op
BenchmarkGoRestful_GithubAll4102913158 ns/op910144 B/op2938 allocs/op
BenchmarkGorillaMux_GithubAll3463384987 ns/op251650 B/op1994 allocs/op
BenchmarkGowwwRouter_GithubAll10000143025 ns/op72144 B/op501 allocs/op
BenchmarkHttpRouter_GithubAll5593821360 ns/op0 B/op0 allocs/op
BenchmarkHttpTreeMux_GithubAll10000153944 ns/op65856 B/op671 allocs/op
BenchmarkKocha_GithubAll10000106315 ns/op23304 B/op843 allocs/op
BenchmarkLARS_GithubAll4777925084 ns/op0 B/op0 allocs/op
BenchmarkMacaron_GithubAll3266371907 ns/op149409 B/op1624 allocs/op
BenchmarkMartini_GithubAll3313444706 ns/op226551 B/op2325 allocs/op
BenchmarkPat_GithubAll2734381818 ns/op1483152 B/op26963 allocs/op
BenchmarkPossum_GithubAll10000164367 ns/op84448 B/op609 allocs/op
BenchmarkR2router_GithubAll10000160220 ns/op77328 B/op979 allocs/op
BenchmarkRivet_GithubAll1462582453 ns/op16272 B/op167 allocs/op
BenchmarkTango_GithubAll6255279611 ns/op63826 B/op1618 allocs/op
BenchmarkTigerTonic_GithubAll2008687874 ns/op193856 B/op4474 allocs/op
BenchmarkTraffic_GithubAll3553478508 ns/op820744 B/op14114 allocs/op
BenchmarkVulcan_GithubAll6885193333 ns/op19894 B/op609 allocs/op
  • (1): 在恆定時間內實現的總重複次數,數字越高表示結果越可信
  • (2): 單次重複持續時間 (ns/op),數字越低表示性能越好
  • (3): 堆內存 (B/op),數字越低表示性能越好
  • (4): 平均分配次數每次重複 (allocs/op),數字越低表示性能越好

中間件

您可以在gin-contrib 中找到許多有用的Gin中間件。

用戶

使用Gin Web框架的優秀項目列表。

  • gorush: 一個使用Go編寫的推送通知服務器。
  • fnproject: 容器原生,雲不可知的無服務器平台。
  • photoprism: 使用Go和Google TensorFlow提供支持的個人照片管理工具。
  • lura: 擁有中間件的超高性能API網關。
  • picfit: 一個使用Go編寫的圖像調整服務器。
  • dkron: 分佈式,容錯的作業調度系統。