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
+4.8k Unix/Linux : secure copying between servers with SCP command examples
+22k Golang : Match strings by wildcard patterns with filepath.Match() function
+12.6k Swift : Convert (cast) Int or int32 value to CGFloat
+12k Golang : md5 hash of a string
+7.9k Golang : Get all countries phone codes
+10.5k Golang : ISO8601 Duration Parser example
+11.3k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+7.2k Golang : File system scanning
+13.7k Golang : unknown escape sequence error
+22.5k Golang : Round float to precision example
+5.7k Golang : Markov chains to predict probability of next state example
+15.1k Golang : Get timezone offset from date or timestamp