Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 39 ยท Getting Started

What Is Gin?

Learn what the Gin web framework is, how it builds on Go's net/http, why it is fast, what it adds (routing, binding, middleware, rendering) and when to choose it for APIs.

Gin is a web framework for Go. It gives you a fast router, a request-handling object called the context, a middleware chain, JSON and form binding with validation, and helpers for rendering responses. It does not replace Go's standard library: Gin is built on top of net/http, so a Gin engine is simply an http.Handler that you can pass to any standard server.

This lesson explains what problem Gin solves, how a request travels through it, how it compares with plain net/http and other frameworks, and what you will build in this tutorial. You should know basic Go first (see the Go tutorial), especially functions, structs and the HTTP server basics.

About the examples. The real Gin library cannot run inside this site's browser runners, because it needs the Go module system and the network to download. Code that uses github.com/gin-gonic/gin is shown in read-only blocks. Next to it you get runnable examples labelled stand-in: small hand-written Go programs that demonstrate the same idea with the standard library only. They show how Gin works inside, and they run for real.

The problem Gin solves

With plain net/http you can already write a server. Here is what a typical JSON endpoint needs when you do it by hand:

Go Stand-in: a plain net/http endpoint

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

That is not bad, but notice the repeated work: setting the content type, writing the status, encoding JSON, reading a query value. Multiply that by fifty endpoints and add logging, authentication, validation and error handling, and you start writing the same helper functions in every project. Gin packages those helpers once, with a consistent API.

What Gin adds

What Gin gives you

Fast router Radix tree, no reflection
Context One object per request
Middleware Chain of reusable steps
Binding JSON, forms, validation

Everything else is plain net/http.

1 / 2
  • Routing. Gin matches the method and path of each request with a radix tree, supports named parameters (/users/:id), wildcards, and route groups.
  • The context (*gin.Context). Each handler receives one object that wraps the request and the response writer, and offers methods such as c.Param, c.Query, c.ShouldBindJSON, c.JSON.
  • Middleware. Cross-cutting behavior (logging, panic recovery, authentication, CORS) is written once and attached to the whole engine, a group, or a single route.
  • Binding and validation. Decode JSON, XML, forms, query strings, URI parameters and headers into structs, and check them with struct tags.
  • Rendering. JSON, XML, YAML, HTML templates, plain text, files, redirects and streaming.

How a request travels through Gin

flowchart LR
    A["Client request"] --> B["net/http server"]
    B --> C["gin.Engine<br/>(http.Handler)"]
    C --> D["Router: find route"]
    D --> E["Middleware 1"]
    E --> F["Middleware 2"]
    F --> G["Your handler"]
    G --> H["c.JSON(...)"]
    H --> B

The engine implements http.Handler, so the standard server calls it for every request.

The server accepts the connection and hands the request to the engine. The router finds the route for the method and path. Gin then runs the chain of functions attached to that route: first the middleware, then your handler. Each function receives the same *gin.Context, which is how they share data.

A first look at real Gin code

Here is the endpoint from above, written with Gin. Do not worry about every detail yet; the next lesson walks through it line by line.

Go
package main

import (
	"net/http"

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

func main() {
	r := gin.Default()

	r.GET("/hello", func(c *gin.Context) {
		name := c.DefaultQuery("name", "world")
		c.JSON(http.StatusOK, gin.H{"message": "hello " + name})
	})

	r.Run() // listens on :8080
}

The handler is shorter and says what it means: read a query value with a default, send JSON with a status. gin.H is just a shortcut for map[string]any.

A stand-in for the context idea

The heart of Gin is the context. This runnable stand-in implements a tiny version of it so you can see that there is no magic: a context is a struct that holds the request and response writer, plus convenience methods.

Go Stand-in: a miniature Context

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The function adapt is the same idea Gin uses internally: it converts a handler that takes a context into a standard http.HandlerFunc. Real Gin also reuses context objects from a sync.Pool, so a request allocates almost nothing, which is a large part of its speed.

Gin compared with other options

Option Style Good for
net/http (Go 1.22+ ServeMux) Standard library, method and wildcard patterns Small services, no dependencies
Gin Context object, rich middleware ecosystem, binding JSON APIs and web apps, the most widely used Go framework
Echo Similar to Gin, slightly different API APIs
Chi Router and middleware that stay close to net/http types Teams who want standard handler signatures
Fiber Express-like, built on fasthttp instead of net/http Very high throughput, but not net/http compatible

Since Go 1.22 the standard library router can do methods and path parameters, so Gin is no longer the only comfortable way to route. Gin's remaining advantages are the batteries: binding with validation, rendering helpers, ready-made middleware, a large community, and a consistent style that is easy to hire for.

What you will learn

This tutorial takes you from go get to a deployed service: your first app, routing, binding and validation, middleware, templates and static files, databases with GORM, authentication and security, testing and deployment, and finally projects.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a *gin.Engine in relation to net/http?

    The engine implements http.Handler, so it plugs into the standard server. Gin is built on net/http, not instead of it.
  2. 2. What does gin.H stand for?

    gin.H is declared as map[string]any and is used to build JSON objects quickly.
  3. 3. Which of these is NOT provided by Gin itself?

    Gin does not include a database layer. You pair it with database/sql or a library such as GORM.
  4. 4. Why can a context be cheap to create for every request?

    Gin keeps a sync.Pool of contexts and resets them for each request, which avoids allocations.

Summary

  • Gin is a web framework built on top of net/http; the engine is an http.Handler.
  • It adds a fast router, a per-request context, middleware, binding with validation, and rendering helpers.
  • Gin has no database, login or project-layout opinions; you add those yourself.
  • Handlers in Gin take a *gin.Context instead of (http.ResponseWriter, *http.Request).
  • The runnable examples in this tutorial are standard-library stand-ins; real Gin code is shown read-only.

Next: Your First Gin App installs Gin and builds a server step by step.