Skip to content

Routing

Gin provides a powerful routing system built on httprouter for high-performance URL matching. Under the hood, httprouter uses a radix tree (also called a compressed trie) to store and look up routes, which means route matching is extremely fast and requires zero memory allocations per lookup. This makes Gin one of the fastest Go web frameworks available.

Routes are registered by calling an HTTP method on the engine (or a route group) and providing a URL pattern along with one or more handler functions:

package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.GET("/hello", func(c *gin.Context) {
c.String(http.StatusOK, "Hello, World!")
})
router.POST("/users", func(c *gin.Context) {
name := c.PostForm("name")
c.JSON(http.StatusCreated, gin.H{"user": name})
})
router.Run(":8080")
}

In this section

The pages below cover each routing topic in detail: