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.4k Golang : Populate slice with sequential integers example
+11.1k Golang : Delay or limit HTTP requests example
+15.8k Golang : Get sub string example
+8.3k Linux/Unix : fatal: the Postfix mail system is already running
+12.6k Golang : http.Get example
+4.9k Golang : Issue HTTP commands to server and port example
+30.6k Golang : Calculate percentage change of two values
+13.5k Golang : How to determine if a year is leap year?
+14.2k How to automatically restart your crashed Golang server
+5.8k Golang : Experimenting with the Rejang script
+13.8k Javascript : Prompt confirmation before exit
+10.9k Google Maps URL parameters configuration