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
+9.3k Golang : does not implement flag.Value (missing Set method)
+30.6k Golang : Generate random string
+32.3k Golang : Convert []string to []byte examples
+27.1k Golang : Force your program to run with root permissions
+4.5k Golang : Valued expressions and functions example
+8.5k Golang : Convert word to its plural form example
+10k Golang : Sort and reverse sort a slice of integers
+8.2k Golang : Variadic function arguments sanity check example
+9.1k Golang : Populate or initialize struct with values example
+11.2k Golang : Read until certain character to break for loop
+9.6k Golang : Accessing content anonymously with Tor
+6.8k Golang : Derive cryptographic key from passwords with Argon2