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
+31.6k Golang : Regular Expression for alphanumeric and underscore
+4.7k Golang : fmt.Println prints out empty data from struct
+12.4k Golang : Get user input until a command or receive a word to stop
+7.4k Golang : Find relative luminance or color brightness
+4.1k PHP : Extract part of a string starting from the middle
+6.2k Golang : Levenshtein distance example
+13.9k Golang : ROT47 (Caesar cipher by 47 characters) example
+9.7k Golang : Create matrix with Gonum Matrix package example
+44.6k Golang : Read tab delimited file with encoding/csv package
+18.7k Golang : How to count the number of repeated characters in a string?
+9.3k Golang : Text file editor (accept input from screen and save to file)
+3.9k Golang : How to pass data between controllers with JSON Web Token