Skip to content

Guide · Open source

How to use Gin: a Go web framework tutorial

Updated · written with AI from the project's own documentation and checked against our catalogue data

Gin is an open-source HTTP web framework written in Go. It offers a Martini-like API with a faster router, and it is aimed at REST APIs, web applications and microservices. This guide shows you how to add Gin to a Go project, run a small server that returns JSON, and decide whether it fits your project.

Gingin-gonic/gin · ★ 89.5k · MIT

Quick start

  1. 1Check prerequisites. Confirm that Go is installed and meets the minimum version in the Gin README.
  2. 2Create a project. Make a new folder for your Go project and create a main.go file inside it.
  3. 3Import Gin. Add import "github.com/gin-gonic/gin" to your code so Go can fetch the package at build time.
  4. 4Write a route. Create a router with gin.Default() and define a GET handler that returns JSON.
  5. 5Run the server. Start the app with go run main.go.
  6. 6Test the endpoint. Open http://localhost:8080/ping in your browser and confirm the response is {"message":"pong"}.

What is Gin and who is it for?

Gin is a Go library for handling HTTP requests. You define routes, write handler functions, and return responses such as JSON or HTML. The project says it provides a Martini-like API with significantly better performance, thanks to httprouter.

It suits Go developers building REST APIs, teams writing microservices in Go, and projects where request throughput matters. It is not a fit for non-programmers, for teams that do not use Go, or for projects that need a full framework with a built-in ORM or admin panel.

What do you need before you start?

You need Go installed. The README says Gin requires the Go version listed in its prerequisites, which is 1.26 or above, so check that your Go toolchain meets it. Basic familiarity with Go syntax and packages also helps.

  • A Go toolchain that meets the minimum version in the Gin README
  • Basic knowledge of Go syntax and package management

How do you install Gin?

Gin uses Go's module support. You don't run a separate install step. Import Gin in your code, and Go fetches it automatically when you build.

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

How do you write and run a first Gin app?

The README example creates a router with the default logger and recovery middleware, defines a GET endpoint at /ping that returns JSON, and starts the server. Save the code as main.go, run it, then open http://localhost:8080/ping in your browser. You should see the JSON message pong.

  • Save the code as main.go
  • Run the application with go run main.go
  • Open http://localhost:8080/ping and check for {"message":"pong"}
package main import ( "log" "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", }) }) if err := r.Run(); err != nil { log.Fatalf("failed to run server: %v", err) } }

What features does Gin provide?

Gin's README lists features for routing, request handling and responses. Middleware can handle tasks such as authentication, logging and CORS. Recovery middleware helps keep panics from stopping the server. Route groups let you apply shared middleware to related routes.

  • Middleware support for authentication, logging, CORS and similar tasks
  • Built-in recovery middleware that prevents panics from crashing the server
  • Route grouping with shared middleware
  • JSON binding and validation for requests
  • Rendering for JSON, XML and HTML templates
  • Centralized error handling and logging

How fast is Gin compared with other Go frameworks?

The README says Gin can be up to 40 times faster than Martini-style frameworks. Its benchmark table uses the GitHub API routing test and compares Gin with frameworks such as Echo, Chi and net/http. Treat these numbers as the project's own results, and run your own tests on your workload before you choose.

Where can you learn more?

The Gin documentation is at gin-gonic.com, with translations in several languages. The examples repository covers REST APIs, authentication, file uploads, WebSockets and template rendering. The Go.dev tutorial walks through building a RESTful API with Gin.

  • Official docs: gin-gonic.com
  • Examples repository: github.com/gin-gonic/examples
  • Go.dev tutorial: Developing a RESTful API with Go and Gin

What people use it for

  • Building REST APIs that return JSON
  • Writing microservices that handle many concurrent requests
  • Serving web applications with HTML templates
  • Adding shared middleware such as logging or authentication to route groups

Frequently asked questions

How do I install Gin?

Import it in your Go code with import "github.com/gin-gonic/gin". Go's module support fetches the package automatically when you build. The README does not describe a separate install step.

What do I need to use Gin?

You need Go, at or above the minimum version listed in the Gin README. Basic Go knowledge is also needed, since Gin is a library you call from code.

Is Gin free to use?

Yes. The repository is licensed under the MIT License, as listed on its GitHub page.

What are the alternatives to Gin?

The README compares Gin with Echo, Chi, Fiber and Go's standard net/http package. Pick based on your team's needs, the API style you prefer, and your own benchmarks.

Is Gin faster than other Go frameworks?

The README says Gin is up to 40 times faster than Martini-style frameworks and shows a GitHub API routing benchmark. Those are the project's results, so test with your own workload before relying on them.

Why won't my Gin app start or respond?

The README does not include a troubleshooting section. Check that Go meets the required version, that the import path is correct, and that port 8080 is free. For more help, see the official docs at gin-gonic.com.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Gin is a good choice if you already write Go and want a fast, focused framework for REST APIs and microservices. If you are not using Go, or you need a full framework with a built-in ORM or admin panel, choose a different tool.

Install options & downloads