Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Golang get mux value

I’m new to go. I’m using mux and trying to parse out a url parameter. I have the following code where r is a *http.Request

vars := mux.Vars(r)
user := vars["user"]
fmt.Print(user)

mux grabs the whole url parameter string like the following print out.

"user=username"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m wondering if it’s possible to simply grab the value (username), or if I need to do regex to get the username.

Thanks for any help!!

>Solution :

Mux’s Vars attribute is more for parsing path variables such as user/{id} and being able to extract out that information. For actual URL query parameters, you’re better off using the built-in request Query() object.

user := r.URL.Query().Get("user")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading