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.9k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+13.3k Golang : Aligning strings to right, left and center with fill example
+26.3k Golang : Remove characters from string example
+13.4k Golang : Display list of time zones with GMT
+6.6k Golang : Generate random Chinese, Japanese, Korean and other runes
+9.2k Golang : Remove or trim extra comma from CSV
+12.5k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+4.3k Golang : How to fix html/template : "somefile" is undefined error?
+6.7k Golang : Populate slice with sequential integers example
+6.5k Your page has meta tags in the body instead of the head
+15.1k Convert JSON to CSV in Golang
+27.2k Golang : Copy directory - including sub-directories and files