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
+21.8k Golang : Match strings by wildcard patterns with filepath.Match() function
+17.3k Golang : delete and modify XML file content
+28.9k Golang : Saving(serializing) and reading file with GOB
+8.7k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+22.9k Golang : Print out struct values in string format
+8.6k Golang : GMail API create and send draft with simple upload attachment example
+21.8k Golang : Securing password with salt
+14.9k Golang : Get all local users and print out their home directory, description and group id
+17.4k Golang : How to make a file read only and set it to writable again?
+11.7k Golang : Convert a rune to unicode style string \u
+10.6k Golang : Get UDP client IP address and differentiate clients by port number