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
+6.3k Golang : Getting Echo framework StartAutoTLS to work
+6.4k Golang : Create zip/ePub file without compression(use Store algorithm)
+8.9k Golang : Channels and buffered channels examples
+7.7k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+17.2k Golang : Send email with attachment
+20.5k Golang : Use TLS version 1.2 and enforce server security configuration over client
+15.5k Golang : How to generate QR codes?
+28k Golang : missing Git command
+5.6k Elasticsearch : Shutdown a local node
+7.1k Golang : Reverse text lines or flip line order example
+9.3k JavaScript/JQuery : Detect or intercept enter key pressed example
+4.4k Golang : If else example and common mistake