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.
Go to file
Leon cap dab5944a7b
test(context): add comprehensive unit tests for Context.File() method (#4307)
* test: add comprehensive unit tests for Context.File() method

- Add TestContextFile with multiple test scenarios in context_test.go
- Add simplified tests in context_file_test.go
- Cover file serving, 404 handling, directory access, HEAD requests, and Range requests
- All tests pass successfully

* fix: resolve gci formatting issues in test files

* fix: correct test file paths and add test file to gin directory

* move test_file.txt to testdata directory as suggested by maintainer

* fix: update test file paths to use testdata directory
2025-07-22 21:38:32 +08:00
.github ci(golangci-lint): update configuration and fix lint issues (#4247) 2025-05-23 14:46:48 +08:00
binding docs: correct article usage in comments (#4301) 2025-07-19 14:58:12 +08:00
codec/json feat: support custom json codec at runtime (#3391) 2025-06-16 23:16:36 +08:00
docs feat: support custom json codec at runtime (#3391) 2025-06-16 23:16:36 +08:00
examples Doc: fix gin example notice syntax (#1814) 2019-03-15 15:39:34 +08:00
ginS ci(golangci-lint): update configuration and fix lint issues (#4247) 2025-05-23 14:46:48 +08:00
internal feat: support custom json codec at runtime (#3391) 2025-06-16 23:16:36 +08:00
render chroe: migrate yaml package to github.com/goccy/go-yaml (#4272) 2025-06-21 12:38:28 +08:00
testdata test(context): add comprehensive unit tests for Context.File() method (#4307) 2025-07-22 21:38:32 +08:00
.gitignore test(git): gitignore add develop tools (#3370) 2024-03-08 15:56:00 +08:00
.golangci.yml refactor: replace interface{} with any in type declarations (#4249) 2025-05-26 23:11:05 +08:00
.goreleaser.yaml chore: refactor configuration files for better readability (#3951) 2024-05-07 10:16:38 +08:00
auth_test.go ci(lint): enable usestdlibvars linter (#4091) 2024-11-15 23:52:16 +08:00
auth.go docs: Add document to constant AuthProxyUserKey and BasicAuthForProxy. (#3887) 2024-03-13 23:22:05 +08:00
AUTHORS.md add v1.8.0 changelog (#3160) 2022-05-30 15:16:10 +08:00
benchmarks_test.go ci(lint): enable usestdlibvars linter (#4091) 2024-11-15 23:52:16 +08:00
BENCHMARKS.md chore: Deleted spaces (#2622) 2021-01-27 09:58:21 +08:00
CHANGELOG.md docs: fix case error of X-Real-IP (#4185) 2025-03-18 22:12:36 +08:00
CODE_OF_CONDUCT.md docs(conduct): add code of conduct 2017-07-05 18:25:37 +02:00
codecov.yml build(codecov): Added a codecov configuration (#3891) 2024-03-21 21:13:56 +08:00
context_appengine.go chore: support min go version 1.18 (#3511) 2023-03-01 10:03:48 +08:00
context_file_test.go test(context): add comprehensive unit tests for Context.File() method (#4307) 2025-07-22 21:38:32 +08:00
context_test.go test(context): add comprehensive unit tests for Context.File() method (#4307) 2025-07-22 21:38:32 +08:00
context.go docs(context): fix AbortWithStatusPureJSON comment typo (#4310) 2025-07-22 21:36:47 +08:00
CONTRIBUTING.md ci: add github action workflows (#2596) 2021-06-23 13:10:49 +08:00
debug_test.go chore: update Go versions and dependencies for improved compatibility (#4187) 2025-03-20 10:13:47 +08:00
debug.go fix: version number discrepancy (#4299) 2025-07-17 19:51:40 +08:00
deprecated_test.go ci(lint): enable usestdlibvars linter (#4091) 2024-11-15 23:52:16 +08:00
deprecated.go docs(context): Added deprecation comments to BindWith (#3880) 2024-03-12 13:55:52 +08:00
doc.go docs: added comment in doc.go (#4274) 2025-07-13 09:43:32 +08:00
errors_test.go feat: support custom json codec at runtime (#3391) 2025-06-16 23:16:36 +08:00
errors.go feat: support custom json codec at runtime (#3391) 2025-06-16 23:16:36 +08:00
fs_test.go ci(lint): enable testifylint linter (#4010) 2024-07-14 20:33:08 +08:00
fs.go ci(golangci-lint): update configuration and fix lint issues (#4247) 2025-05-23 14:46:48 +08:00
gin_integration_test.go ci(golangci-lint): update configuration and fix lint issues (#4247) 2025-05-23 14:46:48 +08:00
gin_test.go ci(golangci-lint): update configuration and fix lint issues (#4247) 2025-05-23 14:46:48 +08:00
gin.go docs: small changes (#4261) 2025-06-09 21:04:23 +08:00
githubapi_test.go ci(golangci-lint): update configuration and fix lint issues (#4247) 2025-05-23 14:46:48 +08:00
go.mod chore(deps): bump golang.org/x/net from 0.41.0 to 0.42.0 (#4297) 2025-07-19 15:08:22 +08:00
go.sum chore(deps): bump golang.org/x/net from 0.41.0 to 0.42.0 (#4297) 2025-07-19 15:08:22 +08:00
LICENSE Add MIT license 2014-06-30 16:54:02 -04:00
logger_test.go refactor(context): refactor Keys type to map[any]any (#3963) 2025-05-26 23:15:14 +08:00
logger.go docs: correct article usage in comments (#4301) 2025-07-19 14:58:12 +08:00
Makefile ci(Makefile): added help and descriptions to targets (#3964) 2024-05-13 09:29:21 +08:00
middleware_test.go ci(golangci-lint): update configuration and fix lint issues (#4247) 2025-05-23 14:46:48 +08:00
mode_test.go fix(gin): data race warning for gin mode (#1580) 2024-05-08 09:13:36 +08:00
mode.go chore(mode): remove impossible case (empty value for mode) (#4303) 2025-07-19 15:07:44 +08:00
path_test.go ci(lint): enable testifylint linter (#4010) 2024-07-14 20:33:08 +08:00
path.go chore: upgrade golangci-lint and fix golangci-lint error (#3278) 2022-08-15 21:38:20 +08:00
README.md docs: added available ID documentation (#4287) 2025-07-13 09:26:26 +08:00
recovery_test.go refactor(recovery): extract Authorization header masking into maskAuthorization func (#4143) 2025-06-02 12:38:19 +08:00
recovery.go refactor(recovery): extract Authorization header masking into maskAuthorization func (#4143) 2025-06-02 12:38:19 +08:00
response_writer_test.go ci(lint): enable testifylint linter (#4010) 2024-07-14 20:33:08 +08:00
response_writer.go add supprt for go1.20 http.rwUnwrapper to gin.responseWriter (#3489) 2023-02-12 13:01:43 +08:00
routergroup_test.go test(route): add some test for routergroup (#4291) 2025-07-19 15:49:41 +08:00
routergroup.go feat(fs): Export, test and document OnlyFilesFS (#3939) 2024-05-08 15:47:54 +08:00
routes_test.go ci(golangci-lint): update configuration and fix lint issues (#4247) 2025-05-23 14:46:48 +08:00
test_helpers.go fix(engine): missing route params for CreateTestContext (#2778) (#2803) 2022-11-06 17:08:11 +08:00
tree_test.go ci(golangci-lint): update configuration and fix lint issues (#4247) 2025-05-23 14:46:48 +08:00
tree.go perf(all): use strings.Cut to replace strings.SplitN (#4239) 2025-05-20 22:58:34 +08:00
utils_test.go ci(golangci-lint): update configuration and fix lint issues (#4247) 2025-05-23 14:46:48 +08:00
utils.go chore: support min go version 1.18 (#3511) 2023-03-01 10:03:48 +08:00
version.go feat: update version constant to v1.10.0 (#3952) 2024-05-07 11:23:42 +08:00

Gin Web Framework

Build Status codecov Go Report Card Go Reference Sourcegraph Open Source Helpers Release TODOs

Gin is a web framework written in Go. It features a martini-like API with performance that is up to 40 times faster thanks to httprouter. If you need performance and good productivity, you will love Gin.

Gin's key features are:

  • Zero allocation router
  • Speed
  • Middleware support
  • Crash-free
  • JSON validation
  • Route grouping
  • Error management
  • Built-in rendering
  • Extensible

Getting started

Prerequisites

Gin requires Go version 1.23 or above.

Getting Gin

With Go's module support, go [build|run|test] automatically fetches the necessary dependencies when you add the import in your code:

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

Alternatively, use go get:

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

Running Gin

A basic example:

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() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

To run the code, use the go run command, like:

go run example.go

Then visit 0.0.0.0:8080/ping in your browser to see the response!

See more examples

Quick Start

Learn and practice with the Gin Quick Start, which includes API examples and builds tag.

Examples

A number of ready-to-run examples demonstrating various use cases of Gin are available in the Gin examples repository.

Documentation

See the API documentation on go.dev.

The documentation is also available on gin-gonic.com in several languages:

Articles

Benchmarks

Gin uses a custom version of HttpRouter, see all benchmarks.

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
  • (1): Total Repetitions achieved in constant time, higher means more confident result
  • (2): Single Repetition Duration (ns/op), lower is better
  • (3): Heap Memory (B/op), lower is better
  • (4): Average Allocations per Repetition (allocs/op), lower is better

Middleware

You can find many useful Gin middlewares at gin-contrib and gin-gonic/contrib.

Uses

Here are some awesome projects that are using the Gin web framework.

  • gorush: A push notification server.
  • fnproject: A container native, cloud agnostic serverless platform.
  • photoprism: Personal photo management powered by Google TensorFlow.
  • lura: Ultra performant API Gateway with middleware.
  • picfit: An image resizing server.
  • dkron: Distributed, fault tolerant job scheduling system.

Contributing

Gin is the work of hundreds of contributors. We appreciate your help!

Please see CONTRIBUTING.md for details on submitting patches and the contribution workflow.