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
+12.8k How to automatically restart your crashed Golang server
+4.9k Golang : Launching your executable inside a console under Linux
+17.4k Golang : Check whether a network interface is up on your machine
+5.6k Golang : When to use make or new?
+23.4k Golang : Create PDF file from HTML file
+8.1k Golang : How to check if a string with spaces in between is numeric?
+13.3k Golang : Convert(cast) int to float example
+15k Golang : Execute terminal command to remote machine example
+35.3k Golang : Convert(cast) int64 to string
+7.4k Golang : Count leading or ending zeros(any item of interest) example
+5.6k Golang : Spell checking with ispell example
+9.8k Golang : Generate random elements without repetition or duplicate