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
+25.1k Golang : Convert uint value to string type
+9.5k Golang : Copy map(hash table) example
+11.9k Golang : Pagination with go-paginator configuration example
+3.6k Java : Random alphabets, alpha-numeric or numbers only string generator
+7.4k Golang : Detect sample rate, channels or latency with PortAudio
+86.7k Golang : How to convert character to ASCII and back
+5.2k Unix/Linux : How to archive and compress entire directory ?
+12.2k Golang : Encrypt and decrypt data with x509 crypto
+12.2k Golang : Extract part of string with regular expression
+21.9k Golang : Match strings by wildcard patterns with filepath.Match() function
+10.9k Golang : Create S3 bucket with official aws-sdk-go package
+40k Golang : Convert to io.ReadSeeker type