package module
v1.0.2 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2023 License: BSD-3-Clause Imports: 6 Imported by: 622



testing codecov godoc sourcegraph

Gorilla Logo


With a properly configured Go toolchain:

go get

Here's an example of a RESTful api:

package main

import (


func homeHandler(wr http.ResponseWriter, req *http.Request) {
	wr.Write([]byte("Yay! We're home, Jim!"))

func getAllTheThings(wr http.ResponseWriter, req *http.Request) {
	wr.Write([]byte("Look, Jim! Get all the things!"))

func putOneThing(wr http.ResponseWriter, req *http.Request) {
	wr.Write([]byte("Look, Jim! Put one thing!"))

func deleteOneThing(wr http.ResponseWriter, req *http.Request) {
	wr.Write([]byte("Look, Jim! Delete one thing!"))

func main() {
	router := pat.New()

	router.Get("/things", getAllTheThings)
	router.Put("/things/{id}", putOneThing)
	router.Delete("/things/{id}", deleteOneThing)
	router.Get("/", homeHandler)

	http.Handle("/", router)

	log.Print("Listening on")
	log.Fatal(http.ListenAndServe(":8000", nil))

Notice how the routes descend? That's because Pat will take the first route that matches.
For your own testing, take the line router.Get("/", homeHandler) and put it above the other routes and run the example. When you try to curl any of the routes, you'll only get what the homeHandler returns.
Design your routes carefully.



Package gorilla/pat is a request router and dispatcher with a pat-like interface. It is an alternative to gorilla/mux that showcases how it can be used as a base for different API flavors. Package pat is documented at:

Let's start registering a couple of URL paths and handlers:

func main() {
	r := pat.New()
	r.Get("/products", ProductsHandler)
	r.Get("/articles", ArticlesHandler)
	r.Get("/", HomeHandler)
	http.Handle("/", r)

Here we register three routes mapping URL paths to handlers. This is equivalent to how http.HandleFunc() works: if an incoming GET request matches one of the paths, the corresponding handler is called passing (http.ResponseWriter, *http.Request) as parameters.

Note: gorilla/pat matches path prefixes, so you must register the most specific paths first.

Note: differently from pat, these methods accept a handler function, and not an http.Handler. We think this is shorter and more convenient. To set an http.Handler, use the Add() method.

Paths can have variables. They are defined using the format {name} or {name:pattern}. If a regular expression pattern is not defined, the matched variable will be anything until the next slash. For example:

r := pat.New()
r.Get("/articles/{category}/{id:[0-9]+}", ArticleHandler)
r.Get("/articles/{category}/", ArticlesCategoryHandler)
r.Get("/products/{key}", ProductHandler)

The names are used to create a map of route variables which are stored in the URL query, prefixed by a colon:

category := req.URL.Query().Get(":category")

As in the gorilla/mux package, other matchers can be added to the registered routes and URLs can be reversed as well. To build a URL for a route, first add a name to it:

r.Get("/products/{key}", ProductHandler).Name("product")

Then you can get it using the name and generate a URL:

url, err := r.GetRoute("product").URL("key", "transmogrifier")

...and the result will be a url.URL with the following path:


Check the mux documentation for more details about URL building and extra matchers:



This section is empty.


This section is empty.


This section is empty.


type Router

type Router struct {

Router is a request router that implements a pat-like API.

pat docs:

func New

func New() *Router

New returns a new router.

func (*Router) Add

func (r *Router) Add(meth, pat string, h http.Handler) *mux.Route

Add registers a pattern with a handler for the given request method.

func (*Router) Delete

func (r *Router) Delete(pat string, h http.HandlerFunc) *mux.Route

Delete registers a pattern with a handler for DELETE requests.

func (*Router) Get

func (r *Router) Get(pat string, h http.HandlerFunc) *mux.Route

Get registers a pattern with a handler for GET requests.

func (*Router) Head

func (r *Router) Head(pat string, h http.HandlerFunc) *mux.Route

Head registers a pattern with a handler for HEAD requests.

func (*Router) Options

func (r *Router) Options(pat string, h http.HandlerFunc) *mux.Route

Options registers a pattern with a handler for OPTIONS requests.

func (*Router) Patch

func (r *Router) Patch(pat string, h http.HandlerFunc) *mux.Route

Patch registers a pattern with a handler for PATCH requests.

func (*Router) Post

func (r *Router) Post(pat string, h http.HandlerFunc) *mux.Route

Post registers a pattern with a handler for POST requests.

func (*Router) Put

func (r *Router) Put(pat string, h http.HandlerFunc) *mux.Route

Put registers a pattern with a handler for PUT requests.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP dispatches the handler registered in the matched route.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL