Efficient URL routing using a Trie data structure.
This Package implements a URL Router, but instead of using the usual "evaluate all the routes and return the first regexp that matches" strategy, it uses a Trie data structure to perform the routing. This is more efficient, and scales better for a large number of routes. It supports the :param and *splat placeholders in the route strings.
This package is "go-gettable", just do:
go get github.com/ant0ine/go-urlrouter
router := urlrouter.Router{
Routes: []urlrouter.Route{
urlrouter.Route{
PathExp: "/resources/:id",
Dest: "one_resource",
},
urlrouter.Route{
PathExp: "/resources",
Dest: "all_resources",
},
},
}
err := router.Start()
if err != nil {
panic(err)
}
input := "http://example.org/resources/123"
route, params, err := router.FindRoute(input)
if err != nil {
panic(err)
}
fmt.Print(route.Dest) // one_resource
fmt.Print(params["id"]) // 123
- Web Server Demo how to use the router with
net/http
- Go-Json-Rest A quick and easy way to setup a RESTful JSON API
- Online Documentation (godoc.org)
- [(Blog Post) Better URL Routing ?] (http://blog.ant0ine.com/typepad/2013/02/better-url-routing-golang-1.html)
- [(Blog Post) Playing with Go1.1beta2, it's much faster!] (http://blog.ant0ine.com/typepad/2013/04/go1-1-beta2-much-faster-.html)
Copyright (c) 2013 Antoine Imbert