github.com/gorilla/pat

Package gorilla/pat is a pretty simple HTTP router for Go.


Keywords
go, golang, gorilla, gorilla-web-toolkit, http, router
License
BSD-3-Clause
Install
go get github.com/gorilla/pat

Documentation

pat

testing codecov godoc sourcegraph

Gorilla Logo

Install

With a properly configured Go toolchain:

go get github.com/gorilla/pat

Example

Here's an example of a RESTful api:

package main

import (
	"log"
	"net/http"

	"github.com/gorilla/pat"
)

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

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

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

func deleteOneThing(wr http.ResponseWriter, req *http.Request) {
	wr.WriteHeader(http.StatusOK)
	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 127.0.0.1:8000...")
	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.