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
+7.1k Gogland : Single File versus Go Application Run Configurations
+28.7k Golang : missing Git command
+17.9k Golang : Find IP address from string
+9.7k Golang : Random Rune generator
+15.9k Golang : Check if a string contains multiple sub-strings in []string?
+11.5k Golang : How to parse plain email text and process email header?
+21.6k Golang : Securing password with salt
+18.7k Golang : Delete item from slice based on index/key position
+18.9k Golang : Get current URL example
+11.2k Get form post value in Go
+21.3k Golang : Join arrays or slices example
+13.9k Golang : GUI with Qt and OpenCV to capture image from camera