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
+5.6k Golang : Function as an argument type example
+11.3k Get form post value in Go
+6.8k Golang : How to fix html/template : "somefile" is undefined error?
+20.9k Golang : Encrypt and decrypt data with TripleDES
+13.3k Golang : Tutorial on loading GOB and PEM files
+13k Golang : Get user input until a command or receive a word to stop
+9.8k Golang : Embed secret text string into binary(executable) file
+13.1k Golang : Strings comparison
+14k Golang : How to check if your program is running in a terminal
+7.9k Golang : Check if integer is power of four example
+33.5k Golang : Call a function after some delay(time.Sleep and Tick)
+31.7k Golang : Convert []string to []byte examples