Golang : Gorrila mux.Vars() function example
Gorrila Web toolkit mux.Vars() function usage example. Vars contains the URI variables of the current request.
For example :
URL : http://website:8080/person/Boo/CEO/199 and processed by
mx.HandleFunc("/person/{name}/{job}/{age:[0-199]+}", ProcessPathVariables)
Vars will returns the name
, job
and age
from r *http.Request
func ProcessPathVariables(w http.ResponseWriter, r *http.Request) {
// break down the variables for easier assignment
vars := mux.Vars(r)
name := vars["name"]
job := vars["job"]
age := vars["age"]
w.Write([]byte(fmt.Sprintf("Name is %s ", name)))
w.Write([]byte(fmt.Sprintf("Job is %s ", job)))
w.Write([]byte(fmt.Sprintf("Age is %s ", age)))
}
Reference :
See also : Golang : Gorilla mux routing example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5k Golang : Detect variable or constant type
+6.4k Golang : How to convert strange string to JSON with json.MarshalIndent
+11.2k Golang : How to display image file or expose CSS, JS files from localhost?
+6.4k Golang : Individual and total number of words counter example
+18.1k Golang : Measure http.Get() execution time
+11.5k Golang : Transform comma separated string to slice example
+4.4k Python : Delay with time.sleep() function example
+13.4k Golang : Execute function at intervals or after some delay
+6.4k Golang : Command line ticker to show work in progress
+17k Golang : How to get hour, minute, second from time?
+6.9k Golang : Add build version and other information in executables