Skip to content

gorilla/pat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

b1685f4 ยท Dec 7, 2023

History

35 Commits
Oct 18, 2023
Nov 5, 2023
Jul 29, 2023
Jul 29, 2023
Jul 29, 2023
Jul 29, 2023
Nov 2, 2023
Nov 12, 2013
Nov 5, 2023
Nov 5, 2023
Dec 7, 2023
Dec 7, 2023

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.