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
+16.5k Golang : Test floating point numbers not-a-number and infinite example
+5.8k Linux : Disable and enable IPv4 forwarding
+6.9k Fix sudo yum hang problem with no output or error messages
+7.1k Restart Apache or Nginx web server without password prompt
+10.1k Golang : Setting variable value with ldflags
+9.4k Golang : Find the length of big.Int variable example
+15.9k Golang : Update database with GORM example
+13.7k Golang : Set image canvas or background to transparent
+23.5k Golang : Get ASCII code from a key press(cross-platform) example
+11k Golang : Replace a parameter's value inside a configuration file example
+5k Python : Convert(cast) bytes to string example
+10.1k Golang : Get login name from environment and prompt for password