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
+12.7k How to automatically restart your crashed Golang server
+12k Golang : Get user input until a command or receive a word to stop
+11.5k Golang : How to calculate the distance between two coordinates using Haversine formula
+9.7k Golang : Sieve of Eratosthenes algorithm
+7.3k Swift : Convert (cast) Character to Integer?
+28.9k Golang : Generate random string
+3.7k Golang : How to pass data between controllers with JSON Web Token
+6.9k Golang : Load DSA public key from file example
+7.7k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+6.7k Golang : Grayscale Image
+6k Golang : Null and nil value
+5.6k Golang : Derive cryptographic key from passwords with Argon2