gin README

The project's README from github.com/gin-gonic/gin. As one markdown file: README.md.

Gin Web Framework

Build Status Trivy Security Scan codecov Go Report Card Go Reference Sourcegraph Open Source Helpers Release

📰 Gin 1.12.0 is now available!

We're excited to announce the release of Gin 1.12.0! This release brings new features, performance improvements, and important bug fixes. Check out the release announcement on our official blog for the full details.


Gin is a high-performance HTTP web framework written in Go. It provides a Martini-like API but with significantly better performance—up to 40 times faster—thanks to httprouter. Gin is designed for building REST APIs, web applications, and microservices where speed and developer productivity are essential.

Why choose Gin?

Gin combines the simplicity of Express.js-style routing with Go's performance characteristics, making it ideal for:

Gin's key features:

Getting Started

Prerequisites

Installation

With Go's module support, simply import Gin in your code and Go will automatically fetch it during build:

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

Your First Gin Application

Here's a complete example that demonstrates Gin's simplicity:

package main

import (
  "log"
  "net/http"

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

func main() {
  // Create a Gin router with default middleware (logger and recovery)
  r := gin.Default()

  // Define a simple GET endpoint
  r.GET("/ping", func(c *gin.Context) {
    // Return JSON response
    c.JSON(http.StatusOK, gin.H{
      "message": "pong",
    })
  })

  // Start server on port 8080 (default)
  // Server will listen on 0.0.0.0:8080 (localhost:8080 on Windows)
  if err := r.Run(); err != nil {
    log.Fatalf("failed to run server: %v", err)
  }
}

Running the application:

  1. Save the code above as main.go

  2. Run the application:

    go run main.go
    
  3. Open your browser and visit http://localhost:8080/ping

  4. You should see: {"message":"pong"}

What this example demonstrates:

Next Steps

After running your first Gin application, explore these resources to learn more:

📚 Learning Resources

📖 Documentation

API Reference

User Guides

The comprehensive documentation is available on gin-gonic.com in multiple languages:

Official Tutorials

⚡ Performance Benchmarks

Gin demonstrates exceptional performance compared to other Go web frameworks. It uses a custom version of HttpRouter for maximum efficiency. View detailed benchmarks →

Gin vs. Other Go Frameworks (GitHub API routing benchmark):

Benchmark name (1) (2) (3) (4)
BenchmarkGin_GithubAll 43550 27364 ns/op 0 B/op 0 allocs/op
BenchmarkAce_GithubAll 40543 29670 ns/op 0 B/op 0 allocs/op
BenchmarkAero_GithubAll 57632 20648 ns/op 0 B/op 0 allocs/op
BenchmarkBear_GithubAll 9234 216179 ns/op 86448 B/op 943 allocs/op
BenchmarkBeego_GithubAll 7407 243496 ns/op 71456 B/op 609 allocs/op
BenchmarkBone_GithubAll 420 2922835 ns/op 720160 B/op 8620 allocs/op
BenchmarkChi_GithubAll 7620 238331 ns/op 87696 B/op 609 allocs/op
BenchmarkDenco_GithubAll 18355 64494 ns/op 20224 B/op 167 allocs/op
BenchmarkEcho_GithubAll 31251 38479 ns/op 0 B/op 0 allocs/op
BenchmarkGocraftWeb_GithubAll 4117 300062 ns/op 131656 B/op 1686 allocs/op
BenchmarkGoji_GithubAll 3274 416158 ns/op 56112 B/op 334 allocs/op
BenchmarkGojiv2_GithubAll 1402 870518 ns/op 352720 B/op 4321 allocs/op
BenchmarkGoJsonRest_GithubAll 2976 401507 ns/op 134371 B/op 2737 allocs/op
BenchmarkGoRestful_GithubAll 410 2913158 ns/op 910144 B/op 2938 allocs/op
BenchmarkGorillaMux_GithubAll 346 3384987 ns/op 251650 B/op 1994 allocs/op
BenchmarkGowwwRouter_GithubAll 10000 143025 ns/op 72144 B/op 501 allocs/op
BenchmarkHttpRouter_GithubAll 55938 21360 ns/op 0 B/op 0 allocs/op
BenchmarkHttpTreeMux_GithubAll 10000 153944 ns/op 65856 B/op 671 allocs/op
BenchmarkKocha_GithubAll 10000 106315 ns/op 23304 B/op 843 allocs/op
BenchmarkLARS_GithubAll 47779 25084 ns/op 0 B/op 0 allocs/op
BenchmarkMacaron_GithubAll 3266 371907 ns/op 149409 B/op 1624 allocs/op
BenchmarkMartini_GithubAll 331 3444706 ns/op 226551 B/op 2325 allocs/op
BenchmarkPat_GithubAll 273 4381818 ns/op 1483152 B/op 26963 allocs/op
BenchmarkPossum_GithubAll 10000 164367 ns/op 84448 B/op 609 allocs/op
BenchmarkR2router_GithubAll 10000 160220 ns/op 77328 B/op 979 allocs/op
BenchmarkRivet_GithubAll 14625 82453 ns/op 16272 B/op 167 allocs/op
BenchmarkTango_GithubAll 6255 279611 ns/op 63826 B/op 1618 allocs/op
BenchmarkTigerTonic_GithubAll 2008 687874 ns/op 193856 B/op 4474 allocs/op
BenchmarkTraffic_GithubAll 355 3478508 ns/op 820744 B/op 14114 allocs/op
BenchmarkVulcan_GithubAll 6885 193333 ns/op 19894 B/op 609 allocs/op

🔌 Middleware Ecosystem

Gin has a rich ecosystem of middleware for common web development needs. Explore community-contributed middleware:

🏢 Production Usage

Gin powers many high-traffic applications and services in production:

🤝 Contributing

Gin is the work of hundreds of contributors from around the world. We welcome and appreciate your contributions! See the full list of contributors.

How to Contribute

Getting Started with Contributing

  1. Check out our CONTRIBUTING.md for detailed guidelines
  2. Join our community discussions and ask questions

All contributions are valued and help make Gin better for everyone!

gin (gin-gonic/gin)

Searching Database…

89.4K ·GitHub
这是什么?Gin 是一个用 Go 编写的高性能 HTTP Web 框架,用于构建 REST API、Web 应用和微服务。
它是框架还是仅仅是路由库?它是一个完整的 Web 框架,包含路由、中间件、JSON 绑定与校验以及渲染功能,底层基于定制版的 httprouter,实现零内存分配的路由。
怎样把 Gin 添加到 Go 项目中?在代码中导入 Gin,Go 的模块支持会在构建时自动下载它;然后用 gin.Default() 创建路由,并定义处理函数,例如从 GET /ping 返回 JSON。
需要哪个版本的 Go?需要 Go 1.25 或更高版本,并且需要基本熟悉 Go 语法和包管理。
采用它时有什么需要注意的坑吗?没有明确列为重大陷阱,但你应当固定使用正确的 Go 工具链版本,因为 Gin 要求 Go 1.25+,这可能会导致旧项目无法正常运行。
Gin 与其他 Go 框架相比有多快?在 GitHub API 路由基准测试中,Gin 每次操作耗时约 27364 纳秒,且没有堆内存分配,远超 Martini(3444706 纳秒/次)和 Gorilla Mux(3384987 纳秒/次)等框架。
开箱即用有哪些中间件?Gin 内置了崩溃恢复中间件,并支持可扩展的中间件系统,可用于身份验证、日志记录和 CORS 等功能,另外还有通过 gin-contrib 和 gin-gonic/contrib 提供的丰富生态。
除了 JSON,Gin 还能渲染什么?Gin 内置支持 JSON、XML、HTML 模板等多种渲染方式,同时支持请求与响应的 JSON 自动绑定和校验。
哪些公司在生产环境中使用 Gin?生产环境用户包括 gorush(推送通知)、fnproject(无服务器平台)、photoprism(照片管理)、lura(API 网关)、picfit(图片处理)和 dkron(任务调度)。
What are similar projects?echo, fiber, chi, hono, martini, mux, go-zero, express
Creator
gin-gonic
Size
10.4 MB
Stars
89.4K
Age
12y
Commits · 90 Days