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
+8.9k Golang : On lambda, anonymous, inline functions and function literals
+8.3k Golang : Get final or effective URL with Request.URL example
+5.3k Golang : Convert lines of string into list for delete and insert operation
+19.4k Golang : Populate dropdown with html/template example
+15.2k Golang : Search folders for file recursively with wildcard support
+10.4k Golang : Convert file content to Hex
+7.4k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+22.5k Golang : Convert Unix timestamp to UTC timestamp
+22.3k Golang : How to run Golang application such as web server in the background or as daemon?
+21.9k Golang : How to reverse slice or array elements order
+7k Default cipher that OpenSSL used to encrypt a PEM file
+24.1k Golang : Find biggest/largest number in array