Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jinzhu/1917427 to your computer and use it in GitHub Desktop.
Save jinzhu/1917427 to your computer and use it in GitHub Desktop.
[PATCH form Gorilla] support pass variables from routes to handler
From a948c13b7073af9a0af4e3615cf4a245056a42f2 Mon Sep 17 00:00:00 2001
From: Jinzhu <wosmvp@gmail.com>
Date: Sun, 26 Feb 2012 23:34:58 +0800
Subject: [PATCH] support pass variables from routes to handler
---
route.go | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/route.go b/route.go
index 8a4b521..064f338 100644
--- a/route.go
+++ b/route.go
@@ -29,6 +29,8 @@ type Route struct {
name string
// Error resulted from building a route.
err error
+ // Pass Variable to handler
+ vars map[string]string
}
// Match matches the route against the request.
@@ -54,6 +56,10 @@ func (r *Route) match(req *http.Request, match *RouteMatch) bool {
if match.Vars == nil {
match.Vars = make(map[string]string)
}
+ for i, v := range r.vars {
+ match.Vars[i] = v
+ }
+
// Set variables.
if r.regexp != nil {
r.regexp.setMatch(req, match, r)
@@ -94,6 +100,18 @@ func (r *Route) GetHandler() http.Handler {
// Name sets the name for the route, used to build URLs.
// If the name was registered already it will be overwritten.
+func (r *Route) Params(vars map[string]string) *Route {
+ if r.vars == nil {
+ r.vars = make(map[string]string)
+ }
+
+ for i, v := range vars {
+ r.vars[i] = v
+ }
+
+ return r
+}
+
func (r *Route) Name(name string) *Route {
if r.name != "" {
r.err = fmt.Errorf("mux: route already has name %q, can't set %q",
--
1.7.9.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment