A library to match URL patterns, gorilla/mux style
- Extract variables from a URL
- Validate a URL against a pattern template
- Match host, path, path prefix, or query parameters
go get -u github.com/libertylocked/urlpattern
p := urlpattern.NewPattern().
Path("/api/events/{id:[0-9]+}")
u, _ := url.Parse("https://super.example.com/api/events/12345")
matches, matched := p.Match(u)
// matched: true
// matches: map[string]string{ "id": "12345" }
p := urlpattern.NewPattern().
Host("{subdomain}.example.com").
Path("/api/events/{id:[0-9]+}")
u, _ := url.Parse("https://super.example.com/api/events/12345")
matches, matched := p.Match(u)
// matched: true
// matches: map[string]string{ "subdomain": "super", "id": "12345" }
p := urlpattern.NewPattern().
PathPrefix("/api")
u, _ := url.Parse("https://super.example.com/api/events/12345")
matches, matched := p.Match(u)
// matched: true
// matches: map[string]string{ }
p := urlpattern.NewPattern().
Queries("key", "{key:[0-9]+}")
u, _ := url.Parse("http://super.example.com/api/events/12345?key=42")
matches, matched := p.Match(u)
// matched: true
// matches: map[string]string{ "key": "42" }
p := urlpattern.NewPattern().
Host("{subdomain}.example.com").
PathPrefix("/api").
Path("/events/{id:[0-9]+}").
Query("key", "{key}")
u, _ := url.Parse("http://super.example.com/api/events/12345?key=42")
matches, matched := p.Match(u)
// matched: true
// matches: map[string]string{ "subdomain": "super", "id": "12345", "key": "42" }