httprouter is a compact router that compiles route templates into minimal-perfect-hash tables at build time so lookups are constant-time.
go get github.com/jeremyctrl/httprouterpackage main
import (
"fmt"
"log"
"net/http"
"github.com/jeremyctrl/httprouter"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}
func main() {
router := httprouter.New().
GET("/", Index).
GET("/hello/:name", Hello).
Build()
log.Fatal(http.ListenAndServe(":8080", router))
}